NASM Forum > Programming with NASM

Linux x86. Align segmtnt elf_i386 (p_align)

(1/1)

AntonPotapov:
ELF_i386 !!!
How do I align segments in the output elf file?
gcc aligns segments to 64 bytes
ld aligns to 4096 bytes.
I found this out by looking at the output program header (p_align)
https://en.wikipedia.org/wiki/Executable_and_Linkable_Format
https://refspecs.linuxbase.org/elf/elf.pdf

I know that a good alignment improves the speed of the program. I don't know elf files that well. But how can I align segments on my own?

ld --help. I found parameters that remove the alignment:
  -n, --nmagic                Do not page align data
  -N, --omagic                Do not page align data, do not make text readonly

It also contains, but different elf formats (not elf_i386).
  --file-alignment <size>            Set file alignment
  --section-alignment <size>         Set section alignment

My usual linking

--- Code: ---#/bin/bash
ld -m elf_i386 -o main main.o

--- End code ---

remove alignment

--- Code: ---#/bin/bash
ld -m elf_i386 -n -o main main.o

--- End code ---

fredericopissarra:
You didn't read a line about what I wrote about sections attributes, did ya?

AntonPotapov:

--- Quote from: fredericopissarra on April 27, 2023, 10:25:45 PM ---You didn't read a line about what I wrote about sections attributes, did ya?

--- End quote ---
I have read about align and section types.
let's say I have a .text section that just ends the program with "1".
I can force align. but this will not affect it, only align more than the alignment specified in p_align

For example let's take the gcc linker where align=64, I can specify 2, 4, ... 32, but this will have no effect on the alignment of the sections, only if I don't specify more than 64 it makes the program much bigger

here's a little code:

--- Code: ---section .text align=4096
global main

main:
. . .
ret

--- End code ---

Navigation

[0] Message Index

Go to full version