Author Topic: alignment in bss and data sections  (Read 22228 times)

Timothee Besset

  • Guest
alignment in bss and data sections
« on: October 03, 2007, 05:44:05 PM »
I can't get data alignment working in the data section on Linux/x86

I'm posting an example below. I always get the bss section correctly aligned, and no matter what, the data section doesn't have proper alignment?

; nasm -f elf test.asm; gcc -m32 -o test test.o

; $ nasm -v
; NASM version 0.98.39 compiled on Aug 29 2007
; $ gcc -v
; Using built-in specs.
; Target: i486-linux-gnu
; Thread model: posix
; gcc version 4.2.1 (Debian 4.2.1-5)

bits 32

global main

; the align are useless here, not getting 16 aligned data
section .data align 16
align 16
idata dw 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000

; the align 16 is not even needed, I always get 16 aligned for this section
section .bss ; align 16
; align 16
bssdata resb 16

section .text

main:
   mov ebx, bssdata
   mov ebx, idata

mov eax, 1
   int 0x80

disassembled to:

0x08048350 :    mov    $0x8049550,%ebx
0x08048355 :    mov    $0x804953c,%ebx
0x0804835a :   mov    $0x1,%eax
0x0804835f :   int    $0x80

Timothee Besset

  • Guest
Re: alignment in bss and data sections
« Reply #1 on: October 03, 2007, 06:32:56 PM »
turns out this works:
section .data align=16

and this:
section .data align = 16
or
section .data align 16
(or using the macro)
doesn't work, doesn't print any warning or error either. duh

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: alignment in bss and data sections
« Reply #2 on: October 03, 2007, 06:49:03 PM »
"; the align are useless here, not getting 16 aligned data
section .data align 16
..."

Try "align=16" instead of "align 16". Arguably a "bug" that Nasm doesn't complain about that, but silently ignores the bad syntax. (this is in section declarations - "align 16" would be right inside the section)

section .data align=16
dummy db 0 ; kill our alignment
align 16
idata ...

According to the manual:

The defaults assumed by NASM if you do not specify the above qualifiers are:

section .text    progbits  alloc  exec    nowrite  align=16
section .rodata  progbits  alloc  noexec  nowrite  align=4
section .data    progbits  alloc  noexec  write    align=4
section .bss     nobits    alloc  noexec  write    align=4
section other    progbits  alloc  noexec  nowrite  align=1

I'm not sure why you're seeing 16 byte alignment on .bss... maybe just lucky?

Best,
Frank