Author Topic: Linux x86. Align segmtnt elf_i386 (p_align)  (Read 9226 times)

Offline AntonPotapov

  • Jr. Member
  • *
  • Posts: 13
Linux x86. Align segmtnt elf_i386 (p_align)
« on: April 27, 2023, 03:31:58 PM »
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: [Select]
#/bin/bash
ld -m elf_i386 -o main main.o

remove alignment
Code: [Select]
#/bin/bash
ld -m elf_i386 -n -o main main.o
« Last Edit: April 27, 2023, 04:21:24 PM by AntonPotapov »

Offline fredericopissarra

  • Full Member
  • **
  • Posts: 373
  • Country: br
Re: Linux x86. Align segmtnt elf_i386 (p_align)
« Reply #1 on: April 27, 2023, 10:25:45 PM »
You didn't read a line about what I wrote about sections attributes, did ya?

Offline AntonPotapov

  • Jr. Member
  • *
  • Posts: 13
Re: Linux x86. Align segmtnt elf_i386 (p_align)
« Reply #2 on: April 28, 2023, 01:07:11 PM »
You didn't read a line about what I wrote about sections attributes, did ya?
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: [Select]
section .text align=4096
global main

main:
. . .
ret