How to specify alignment of ELF section in GNU as?

I am trying to use GNU asas a universal assembler that is used as nasm. I create the template source as follows:

.section .text
.globl _start
.intel_syntax noprefix
_start:
call 0xb77431c0 # the instruction I want to assemble

And then I run the build command as follows:

as --32 -o test.o test.s
ld -m elf_i386 -Ttext 0xb77431d9 --oformat binary -o test.bin test.o

Everything works well with binutils 2.24. But it seems that asfrom binutils 2.22 (the one in Ubuntu Precise) aligns the partition .textto a 4-byte border, so instead of the expected disassembly, I get the wrong results:

 # expected and working in binutils 2.24
$ ndisasm -b 32 -o 0xb77431d9 test.bin
B77431D9  E8E2FFFFFF        call dword 0xb77431c0

 # actual in binutils 2.22
$ ndisasm -b 32 -o 0xb77431d9 test.bin
B77431D9  90                nop
B77431DA  90                nop
B77431DB  90                nop
B77431DC  E8DFFFFFFF        call dword 0xb77431c0

The problem is the command as(i.e. not ld): readelf -Sgives me the following as2.22 results :

$ readelf -S test.o | grep ' \.text'
  [ 1] .text             PROGBITS        00000000 000034 000005 00  AX  0   0  4

And for 2.24 I have

$ readelf -S test.o | grep ' \.text'
  [ 1] .text             PROGBITS        00000000 000034 000005 00  AX  0   0  1

, .text. .align 0 .align 1 , .

, : ELF GNU?

+1
2

, script LD - . script , ( ) .

linker.ld

SECTIONS
{
    . = 0xb77431d9;
    .text . : SUBALIGN(0)
    {
        *(.text)
    }
}

script SUBALIGN .text, . GNU Linker :

3.6.8.4

, SUBALIGN.    , , .

:

as --32 -o test.o test.s
ld -T linker.ld -m elf_i386 --oformat binary -o test.bin test.o

, :

ndisasm -b 32 -o 0xb77431d9 test.bin
B77431D9  E8E2FFFFFF        call dword 0xb77431c0

(-) script, . , .

+2

(v3), script :

SECTIONS
{
    . = 0xb77431d9;
    .text . : SUBALIGN(0)
    {
        *(.text)
    }
}

:

$ as --32 -o test.o test.s
$ ld -m elf_i386 -T linker.ld --oformat binary -o test.bin test.o
$ ndisasm -b 32 -o 0xb77431d9 test.bin
B77431D9  E8E2FFFFFF        call dword 0xb77431c0

, .

+1

Source: https://habr.com/ru/post/1672049/


All Articles