Author Topic: section .bss alignment to 32  (Read 9794 times)

Offline stressful

  • Full Member
  • **
  • Posts: 105
  • Country: 00
    • CPU2.0
section .bss alignment to 32
« on: May 01, 2017, 04:45:28 PM »
Upon investigation, I found out that .bss section is actually relative to the .data section (on Windows64). That is to say that to align the .bss (32 in this example below), one would have to do it from inside the .data section and not from the .bss itself.

Code: [Select]
;nasm -f win64 prog.asm
;golink /console prog.obj base6.dll
global Start

extern dumpreg
extern exitx

section .data
y db 'h',0ah,0
align 32     ;this sets alignment for .bss

section .bss align=32 ;this doesn't
;align 32    ;neither does this
x: resb 24

section .code
Start:
        mov     rax,Start    ;check Start
        mov     rbx,y        ;check .data
        mov     rcx,x        ;check .bss
        call    dumpreg
        call    exitx

Output (alignment set in .data):
Code: [Select]
RAX|0000000000401000 RBX|0000000000402000 RCX|0000000000402020 ;this
RDX|0000000000401000 RSI|0000000000000000 RDI|0000000000000000
R8 |00000000003EE000 R9 |0000000000401000 R10|0000000000000000
R11|0000000000000000 R12|0000000000000000 R13|0000000000000000
R14|0000000000000000 R15|0000000000000000 RBP|0000000000000000
RSP|000000000014FF58 RIP|000000000040101E

if compared to this output (alignment 32 sets in .bss itself)
Code: [Select]
RAX|0000000000401000 RBX|0000000000402000 RCX|0000000000402004 ;this
RDX|0000000000401000 RSI|0000000000000000 RDI|0000000000000000
R8 |00000000003EE000 R9 |0000000000401000 R10|0000000000000000
R11|0000000000000000 R12|0000000000000000 R13|0000000000000000
R14|0000000000000000 R15|0000000000000000 RBP|0000000000000000
RSP|000000000014FF58 RIP|000000000040101E

I maybe wrong, but this doesn't look right either.

The library "base6.dll" being used to test the parameters is from BASELIB

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: section .bss alignment to 32
« Reply #1 on: May 01, 2017, 05:18:53 PM »
Last I knew, ".code" was not one of the "known" section names. Try:
Code: [Select]
section .text
The section names are case sensitive, and the leading period is required. I don't know if that'll make a difference to your observation or not.

Best,
Frank


Offline stressful

  • Full Member
  • **
  • Posts: 105
  • Country: 00
    • CPU2.0
Re: section .bss alignment to 32
« Reply #2 on: May 01, 2017, 10:03:33 PM »
Last I knew, ".code" was not one of the "known" section names. Try:
Code: [Select]
section .text
The section names are case sensitive, and the leading period is required. I don't know if that'll make a difference to your observation or not.

Best,
Frank

section name makes no difference in this particular code / example. Section flag marked it accordingly as an executable. I think it's a habit I developed from other syntax.

I've gone thru the NASM manual and didn't find any mention of .bss alignment support larger than 32. Or perhaps I missed that part. Now the question is whether .bss segment does naturally support YMM load and store which uses align=32 because there's no way I can directly do that using .bss. If such the case, then the method of aligning it from inside the .data section or prior to .bss is probably a working / temporary 'tweak'. Something like

Code: [Select]
section .bss
tempYMM resq 4

section .data
aData db 'A'
align 32   ;align the .bss

or simply

Code: [Select]
align 32
section .bss
tempYMM resq 4

I don't know. There's still many more features of NASM that are not known to me at this point.

Offline stressful

  • Full Member
  • **
  • Posts: 105
  • Country: 00
    • CPU2.0
Re: section .bss alignment to 32
« Reply #3 on: May 01, 2017, 11:01:15 PM »
On the contrary, this works as expected on Linux64

Code: [Select]
; nasm -f elf64 prog.asm
; ld prog.o base6.o -o prog

global _start

extern dumpreg
extern exitx

section .data align=32
s db 'hello world',0ah,0

section .bss align=32
;align 32
y resq 4

section .text align=32
_start:
        mov     rax,y
        mov     rbx,s
        mov     rcx,_start
        call    dumpreg
        call    exitx

with this output

Code: [Select]
RAX|0000000000605340 RBX|0000000000600100 RCX|00000000004000C0
RDX|0000000000000000 RSI|0000000000000000 RDI|0000000000000000
R8 |0000000000000000 R9 |0000000000000000 R10|0000000000000000
R11|0000000000000000 R12|0000000000000000 R13|0000000000000000
R14|0000000000000000 R15|0000000000000000 RBP|0000000000000000
RSP|00007FFE994B9630 RIP|00000000004000DE

All sections are well-aligned to 32. And also from the output, one can see that the .bss is not relative to the .data section (.bss and .data are well separated from each other).

Perhaps it has something to do with the COFF format (.obj)? I don't know. If it isn't, then I think it's a flaw, on Windows64.

Offline stressful

  • Full Member
  • **
  • Posts: 105
  • Country: 00
    • CPU2.0
Re: section .bss alignment to 32
« Reply #4 on: May 03, 2017, 11:29:35 AM »
I think that GoLink has a different idea with memory allocation for the .bss and / or .data. I tried with GCC and LINK and it's working just fine. My take that GoLink combines these two (.bss and .data) into the same section and ignoring the alignment requirement for the .bss or at least takes alignment information from the .data section only. Hope someone can confirm this. I am thinking of ditching GoLink support for my source if such is the case. Can't take that risk.



 

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: section .bss alignment to 32
« Reply #5 on: May 03, 2017, 05:13:53 PM »
I dunno, stressful. ELF32/64 is a very different format from win32/64, obviously. Seems to me that in the case of win32 anyway, sections go in the disk file on 400h alignment and the loader moves 'em to 1000h alignment in memory. That should preserve your 32 byte alignment, I would think. Reading through the Friendly Manual, I see that we're supposed to use "alignb" in section .bss. I don't think that's your problem - might be worth a try? I've never used GoLink, and unfortunately we don't have the source. It does some weird things. In particular, the "/mix" switch alters the way things are renamed. It is quite popular, and I don't think it would be if it messed up your alignment. If you're getting what you expect from ld, maybe GoLink is the problem. I dunno.

Best,
Frank


Offline stressful

  • Full Member
  • **
  • Posts: 105
  • Country: 00
    • CPU2.0
Re: section .bss alignment to 32
« Reply #6 on: May 05, 2017, 12:14:45 PM »
Hi Frank, below is two output for GoLink and "ld" each. You see, with "ld", the three sections are all well-aligned to 3 different page boundaries (text=rax=1000h, data=2000h,bss=3000h). With GoLink, the .bss alignment dont seem to work.

Code: [Select]
D:\NASM>golink /console prog.obj base6.dll   ;using golink. Attention to RBX and RCX
D:\NASM>prog
RAX|0000000000401000 RBX|0000000000402000 RCX|0000000000402010
RDX|0000000000401000 RSI|0000000000000000 RDI|0000000000000000
R8 |00000000003F7000 R9 |0000000000401000 R10|0000000000000000
R11|0000000000000000 R12|0000000000000000 R13|0000000000000000
R14|0000000000000000 R15|0000000000000000 RBP|0000000000000000
RSP|000000000014FF58 RIP|000000000040101E

D:\NASM>ld prog.obj base6.dll -o prog.exe
D:\NASM>prog
RAX|0000000000401000 RBX|0000000000402000 RCX|0000000000403000
RDX|0000000000401000 RSI|0000000000000000 RDI|0000000000000000
R8 |00000000003B1000 R9 |0000000000401000 R10|0000000000000000
R11|0000000000000000 R12|0000000000000000 R13|0000000000000000
R14|0000000000000000 R15|0000000000000000 RBP|0000000000000000
RSP|000000000060FF58 RIP|000000000040101E

With golink, alignb or align dont seem to work well. The only way it works is by setting the 32-byte alignment from inside the .data section, which is weird IMHO. Have no problems using other linkers. 

The test code;
Code: [Select]
global Start

section .data
s db 'hello world',0ah,0
;align 32  ;this works for below

section .bss alignb=32  ;this wont do
f resb 5

section .text
Start:
        mov     rax,Start
        mov     rbx,s
        mov     rcx,f
        call    dumpreg
        call    exitx

extern dumpreg
extern exitx