The World of Linkers/ Theory/ 17 articles
45 min readPublic

[The World of Linkers—Theory 02] An Object File Is a Program with Unfinished Business

Separate compilation lets one source file refer to a function defined elsewhere without knowing its final address. The compiler and assembler produce a relocatable object file, conventionally given a .o suffix. Alongside machine code, it preserves symbols and relocation records so the linker can complete those references.

The file also has to distinguish executable instructions, writable variables, read-only constants, and information used only by tools. A large zero-initialized array should not require a large run of zero bytes on disk. An ELF1 object is therefore both compiled material and a structured handoff.

This chapter follows that handoff from assembly directives to raw ELF bytes, explaining how ELF preserves code, data, names, and unfinished address calculations. The examples use little-endian x86-64 ELF64; generic file structures and architecture-specific encodings are treated separately.

Follow an entry from the file to its payload

ELF64 file regions, section table, and complete entry layout

The first row is one contiguous file. The ELF header locates the whole section table: e_shoff=256, e_shnum=3, and e_shstrndx=2. Each entry occupies e_shentsize=64 bytes. Entry 1 starts at 256 + 1 * 64 = 320, so its descriptor occupies [320,384). Reading that entry's sh_offset=64 and sh_size=128 then identifies the .text payload at [64,192). Each of those fields occupies 8 bytes; the stored value describes a different region. Every entry shares the same 64-byte layout, with special SHT_NULL semantics for entry zero.

Names are not inline in the headers. e_shstrndx selects the header describing .shstrtab; that header locates the name bytes. Each entry's sh_name is an offset into those bytes. Keep the two origins distinct: sh_offset is relative to the file start, whereas sh_name is relative to the string-table start.

The section table need not be last, and payload order can differ from table order. SHT_NOBITS, commonly used for .bss, describes memory without storing payload bytes. Three headers take 192 bytes even in this tiny example, so the table can be larger than the payloads it describes.

Start before the object file exists

Use two source files. The caller contains several kinds of storage so that the resulting sections have something concrete to describe:

// main.c
int add(int a, int b);
int counter = 42;
int zeros[64];
const int table[4] = {1, 2, 3, 4};
const char *msg = "hello";
int main(void) {
return add(counter, table[2]) * 2;
}
// add.c
int add(int a, int b) {
return a + b;
}

The command name cc2 does not identify a particular implementation. Here we select Clang explicitly and ask it to show the compilation phases:

$ clang -O1 -ccc-print-phases -c add.c
+- 0: input, "add.c", c
+- 1: preprocessor, {0}, cpp-output
+- 2: compiler, {1}, ir
+- 3: backend, {2}, assembler
4: assembler, {3}, object

Preprocessing expands includes and macros. The compiler produces IR3, optimization works on that representation, the backend selects target instructions, and assembly forms the object file. Save the intermediate products to inspect them:

$ clang -O1 -save-temps -c add.c
$ ls add.*
add.bc add.c add.i add.o add.s

The .i file is preprocessed C, .bc is LLVM4 bitcode, .s is assembly text, and .o is the object. These files make the stages visible; they are not proof that every ordinary compilation writes each intermediate file. Clang can use an integrated assembler. GCC5 commonly invokes as6, with a temporary file or a pipe carrying assembly between stages.

The generated add.s, with a few incidental comments removed, is short:

.file "add.c"
.text
.globl add
.p2align 4
.type add,@function
add:
.cfi_startproc
leal (%rdi,%rsi), %eax
retq
.Lfunc_end0:
.size add, .Lfunc_end0-add
.cfi_endproc
.ident "Ubuntu clang version 21.1.8 (6ubuntu1)"
.section ".note.GNU-stack","",@progbits
.addrsig

Only leal and retq are CPU instructions. In this AT&T syntax, the destination operand is on the right. The x86-64 calling convention supplies the first integer arguments in the argument registers; the result is returned in %eax.

The other lines tell the assembler how to organize the file. A label assigns a name to the current location. .file records a source filename. .ident supplies a compiler-identification string. .cfi_* describes unwind state. .addrsig requests address-significance metadata used when deciding whether merging functions would alter observable address identity. Their consumers differ, even though they arrive in one assembly stream.

The assembler has a current section and a current offset

A useful working model is a collection of growing buffers, one per section. An instruction appends its encoding to the current buffer. A data directive appends data. A label records a section and an offset. Switching away from a section and later returning resumes that same section; it does not create a new copy.

Write an object directly in assembly to see the model without C in front of it:

# hand.S: construct an object directly
.section .text,"ax",@progbits
.globl add
.type add,@function
add:
leal (%rdi,%rsi), %eax
ret
.size add, . - add
.section .data,"aw",@progbits
.balign 4
.globl answer
.type answer,@object
answer:
.long 42
.size answer, 4
.section .rodata,"a",@progbits
greeting:
.asciz "hi"
.byte 0xff
.balign 8
.uleb128 624485
.sleb128 -123456
# Macro: substitute the parameters \name and \value
.macro CONST name, value
.globl \name
\name:
.long \value
.endm
#ifdef BIG
CONST limit, 100000
#else
CONST limit, 100
#endif
.section .note.GNU-stack,"",@progbits

The main directives express different kinds of information:

DirectiveEffect
.section name,"flags",@typeSelect a section, creating it if necessary
.globl nameGive a name global binding
.type name,@function or @objectDescribe what the symbol represents
.size name, expressionRecord a size; . denotes the current location
.long, .quad, .byteEmit fixed-width data of 4, 8, or 1 byte
.ascizEmit a string and a terminating zero
.zero nReserve or emit zero-filled storage according to the section type
.balign 8Advance to a multiple of eight, adding padding
.p2align 4Align to 24=162^4=16 bytes

For the section flags, a means allocated at runtime, w writable, and x executable instructions. @progbits stores contents in the file; @nobits describes storage without corresponding contents. Usual .text, .data, and .bss directives select conventional sections with appropriate defaults. See the assembler's section directive documentation for the complete rules.

In .size add, . - add, both locations belong to the same section, so their difference is the function length. The CPU does not read that size to execute ret; tools use it to identify the function's range.

Alignment can add bytes without adding a source-level object. Data padding here is zero; code alignment may use NOP encodings. The macro body substitutes \name and \value at each invocation. CONST limit, 100 emits a global label followed by a four-byte integer; it does not create a runtime macro mechanism.

For the first pass, retain three ideas: sections group contents, labels name section offsets, and alignment can add padding. The LEB128 and preprocessing directives in hand.S are explained in the optional section near the end.

Which references can the assembler finish?

Knowing that a label occurs in this source file does not always mean its address can be committed now. Consider a local function, a public function, and a global variable:

// calls.c
static __attribute__((noinline)) int helper(int x) { return x * 3; }
__attribute__((noinline)) int pub(int x) { return x + 1; }
int count;
int caller(int x) {
return helper(x) + pub(x) + count;
}

noinline keeps the calls visible for inspection. Add -r to disassembly so relocation records appear beside their fields:

$ clang -O1 -c calls.c -o calls.o
$ llvm-objdump -dr calls.o
[excerpt]
0000000000000010 <caller>:
10: 55 pushq %rbp
11: 53 pushq %rbx
12: 50 pushq %rax
13: 89 fb movl %edi, %ebx
15: e8 26 00 00 00 callq 0x40 <helper>
1a: 89 c5 movl %eax, %ebp
1c: 89 df movl %ebx, %edi
1e: e8 00 00 00 00 callq 0x23 <caller+0x13>
000000000000001f: R_X86_64_PLT32 pub-0x4
23: 01 e8 addl %ebp, %eax
25: 03 05 00 00 00 00 addl (%rip), %eax # 0x2b <caller+0x1b>
0000000000000027: R_X86_64_PC32 count-0x4
[excerpt]
0000000000000040 <helper>:
40: 8d 04 7f leal (%rdi,%rdi,2), %eax
43: c3 retq

The call to helper already has displacement 0x26: the next instruction is at 0x1a, and 0x1a + 0x26 = 0x40. Moving the entire .text section preserves that distance. The call to pub retains a relocation, as does the access to count.

A forward reference such as helper requires layout before final encoding. Even layout can require iteration on x86 because jumps have short and long forms:

$ cat jmp100.s
.text
jmp .Ldone
.fill 100, 1, 0x90
.Ldone:
ret
$ clang -c jmp100.s -o jmp100.o
$ llvm-objdump -d jmp100.o | grep -E 'jmp|ret'
0: eb 64 jmp 0x66 <.text+0x66>
66: c3 retq

A jump over 100 bytes fits the short form eb 64. Change the gap to 200 and the assembler needs the five-byte form e9 c8 00 00 00; the destination itself consequently moves. Multiple branches and alignment directives can influence one another. Iteration finds a stable layout before final fixups are applied.

The assembler initially represents unresolved expressions as fixups. After layout, it can evaluate an expression, reject an invalid expression, or emit a relocation for the linker. Three cases explain this example:

  • A same-section relative reference with established local binding can become a constant displacement.
  • A reference across sections needs their eventual relative placement, even if the target is local.
  • A reference through a global or weak name may require the linker's binding decision, even if a definition is currently visible in the same section.

Give every function its own section and the local call now needs a relocation too:

$ clang -O1 -ffunction-sections -c calls.c -o calls-fs.o
$ llvm-objdump -dr calls-fs.o | grep -A1 'callq'
5: e8 00 00 00 00 callq 0xa <caller+0xa>
0000000000000006: R_X86_64_PLT32 .text.helper-0x4
--
e: e8 00 00 00 00 callq 0x13 <caller+0x13>
000000000000000f: R_X86_64_PLT32 pub-0x4

The reference to helper is expressed as a section symbol plus an offset. Public pub retains its name because symbol resolution and interposition can determine a different binding. By contrast, a same-section difference used for .size can still be evaluated even when one endpoint has global binding: it describes this assembled range.

The rule is about what remains undecided, not whether a human can see both labels in the file.

Read the header before interpreting the contents

Complete ELF64 header byte layout, with file offsets and field widths

The header declares a format before it locates any contents. Class and data encoding in the 16-byte e_ident array establish how later fields are represented. Numbers above the boxes are file offsets; numbers below are field widths. Each row covers a contiguous interval of its own, so a different row length does not change a field’s width. EI_VERSION at byte 6 belongs to the identification array; e_version at [20,24) belongs to the header body. They are distinct version fields.

e_phoff/e_phnum/e_phentsize describe the program-header table; e_shoff/e_shnum/e_shentsize describe the section-header table. Each group supplies a table origin, count, and entry width, rather than a section’s payload location. ET_REL normally has no program-header table. Extended numbering requires consulting section entry zero instead of treating every count field as a final count. See the ELF header specification.

Recognizing format values in the header

Interpret each number in its own field. Class value 2 and e_type value 2 are unrelated. PIE also uses ET_DYN, so that value alone does not identify a shared library.

ValueStandard nameMeaning
1 / 2ELFCLASS32 / ELFCLASS6432-bit / 64-bit file structures
1 / 2ELFDATA2LSB / ELFDATA2MSBLittle-endian / big-endian encoding
1EV_CURRENTCurrent ELF version, used by both version fields
0 / 1 / 2 / 3 / 4ET_NONE / ET_REL / ET_EXEC / ET_DYN / ET_COREUnspecified / relocatable / executable / shared-object format / core dump
3 / 62 / 183 / 243EM_386 / EM_X86_64 / EM_AARCH64 / EM_RISCVTarget machine identifiers, separate from file class
0 / 3ELFOSABI_SYSV / ELFOSABI_GNUSystem V / GNU extension identifiers; Linux files need not use 3

ELF header definitions.

Keep a map in view: the ELF header locates the section-header table; section headers identify contents and byte ranges; .symtab gets names through .strtab; relocation entries refer to symbol-table indices. Each field below describes either a range, a name, or a relationship between tables.

Compile the original inputs:

$ clang -O1 -c main.c -o main.o
$ clang -O1 -c add.c -o add.o
$ file main.o add.o
main.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
add.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped

file identifies class, byte order, machine, and relocatable type from the header. For deeper inspection, GNU readelf7 prints ELF fields directly; llvm-objdump is especially useful for disassembly; nm8 focuses on symbols. Independent readers help cross-check an interpretation. The short Python reader below is an explanatory tool for this known ELF64 little-endian input, not a hardened parser for arbitrary files.

The first 64 bytes are the ELF64 header:

$ xxd -l 64 main.o
00000000: 7f45 4c46 0201 0100 0000 0000 0000 0000 .ELF............
00000010: 0100 3e00 0100 0000 0000 0000 0000 0000 ..>.............
00000020: 0000 0000 0000 0000 e002 0000 0000 0000 ................
00000030: 0000 0000 4000 0000 0000 4000 0f00 0100 ....@.....@.....

The ELF64 structure Elf64_Ehdr occupies 64 bytes; the ELF Header specification defines its fields. The gABI defines the generic file structures, while a psABI9 supplies processor-specific conventions. Class and byte order come from the file's e_ident, rather than from the machine running the parser.

The first 16 bytes, e_ident, can be interpreted before word size or byte order is known. The remaining multibyte fields follow the byte order it specifies. This table accounts for all 64 bytes; offsets are relative to the file start, widths describe the fields themselves, and values are decoded from the example above.

Offset: decimal / hexadecimalWidthFieldExample valueMeaning
0 / 0x004EI_MAG0..EI_MAG37f 45 4c 46ELF magic
4 / 0x041EI_CLASS2ELF64 format
5 / 0x051EI_DATA1Little-endian encoding
6 / 0x061EI_VERSION1Format version in the identification array
7 / 0x071EI_OSABI0OS/ABI extension identity
8 / 0x081EI_ABIVERSION0Version of the identified ABI
9 / 0x097Reserved area beginning at EI_PADAll zeroReserved bytes
16 / 0x102e_type1ET_REL: relocatable file
18 / 0x122e_machine62EM_X86_64: target architecture
20 / 0x144e_version1Format version in the header structure
24 / 0x188e_entry0Entry virtual address; zero if none
32 / 0x208e_phoff0Program-header table file offset; zero if absent
40 / 0x288e_shoff736Section-header table file offset
48 / 0x304e_flags0Processor-specific flags
52 / 0x342e_ehsize64Size of the ELF header itself
54 / 0x362e_phentsize0Size of one program header
56 / 0x382e_phnum0Number of program headers
58 / 0x3a2e_shentsize64Size of one section header
60 / 0x3c2e_shnum15Number of section headers
62 / 0x3e2e_shstrndx1Section-header index of the section-name string table

The two version fields are distinct: EI_VERSION at offset 6 is one byte, while e_version at offset 20 is four bytes. Both contain EV_CURRENT=1 here. Likewise, e_ehsize=64 describes the file header and e_shentsize=64 describes one section header. Equal sizes do not make them the same structure. With ordinary numbering, section indices start at zero and must be less than e_shnum.

Ordinary section counts are below SHN_LORESERVE=0xff00. At that threshold, e_shnum holds zero and section zero's sh_size carries the actual count; the large count is not stored directly in the 16-bit field. A name-table index at the same threshold uses SHN_XINDEX=0xffff in e_shstrndx and section zero's sh_link for the actual index. See the gABI ELF header definition.

ELF also permits an absent section table, an absent section-name table, and extended numbering. A zero e_shnum can indicate no table or require the actual count to be read from entry zero's sh_size; when e_shstrndx=SHN_XINDEX, entry zero's sh_link holds the actual index. A parser can explicitly support only a subset of these encodings without declaring the other encodings invalid ELF.

Other familiar types include ET_EXEC and ET_DYN; the latter covers shared objects and PIE10 executables. Table location, entry count, and the contents described by those entries remain separate concepts.

The section header table ends at 736+15×64=1696736+15\times64=1696, exactly the recorded file size:

$ wc -c main.o
1696 main.o

Proving that a table range can be read

A table position does not prove its bytes exist. Treat a file of length L as a byte sequence and use half-open intervals [offset, end): the start is included, the end is excluded, and the length is end − offset. Reading s bytes at o requires calculating end = o + s without overflow, then proving end ≤ L. With a nonnegative size and valid addition, the start is also within the file. The empty interval [L,L) is valid; [L+1,L+1) is outside the file despite containing no bytes.

The table length is entry count × entry width, not just the count. Here 15 × 64 = 960 bytes, so the table occupies [736,1696). Checking only 736 + 15 ≤ L establishes at most the first fifteen bytes, not fifteen complete headers.

Selecting entry i requires two independent checks:

  1. i < e_shnum: the index belongs to this table. Additional file bytes cannot turn the following content into another table entry.
  2. The entire interval [e_shoff + i × e_shentsize, e_shoff + (i+1) × e_shentsize) lies within the file. A valid index can still refer to truncated input.

Validation of another byte sequence does not establish these facts if the caller supplies a shorter input or constructs a table position itself. Perform arithmetic in a type wide enough for file coordinates. The count and ordinary index fields occupy two bytes, but multiplication need not use a 16-bit type: 1024 × 64 = 65536 cannot be represented in u16. Widen before multiplying rather than reporting a valid count as overflow.

Arithmetic overflow and file bounds are different failures. At offset u64::MAX − 63, a 128-byte table has a mathematical end beyond u64::MAX; addition cannot express its range. At offset 160 with length 64 in a 192-byte file, the end 224 is representable but outside the file. Casting a large offset to host usize first can truncate high bits on a narrower machine and turn an invalid position into an apparently readable small index. Prove the range first, then use checked conversions.

Range checks establish safe reading, not correct record semantics. Name indices, symbol-table associations, and relocation ownership need their own validation; the following sections explain those relationships.

The header summary and empty execution view agree:

$ llvm-objdump -f main.o
main.o: file format elf64-x86-64
architecture: x86_64
start address: 0x0000000000000000
$ llvm-objdump -p main.o
main.o: file format elf64-x86-64
Program Header:
Dynamic Section:

The section view, however, is already populated:

$ llvm-objdump -h main.o
main.o: file format elf64-x86-64
Sections:
Idx Name Size VMA Type
0 00000000 0000000000000000
1 .strtab 000000a1 0000000000000000
2 .text 00000015 0000000000000000 TEXT
3 .rela.text 00000030 0000000000000000
4 .data 00000010 0000000000000000 DATA
5 .rela.data 00000018 0000000000000000
6 .rodata 00000010 0000000000000000 DATA
7 .rodata.str1.1 00000006 0000000000000000 DATA
8 .bss 00000100 0000000000000000 BSS
9 .comment 00000028 0000000000000000
10 .note.GNU-stack 00000000 0000000000000000
11 .eh_frame 00000030 0000000000000000 DATA
12 .rela.eh_frame 00000018 0000000000000000
13 .llvm_addrsig 00000000 0000000000000000
14 .symtab 000000f0 0000000000000000

Zero section virtual addresses are expected here: these input sections have not received final runtime placement.

Section headers describe relationships as well as ranges

Each section header uses the 64-byte Elf64_Shdr layout. Offsets here are relative to the beginning of that entry, rather than to the beginning of the file.

Entry offset: decimal / hexadecimalWidthFieldMeaning
0 / 0x004sh_nameByte offset within the section-name string table
4 / 0x044sh_typeSection type
8 / 0x088sh_flagsSection attribute bits
16 / 0x108sh_addrMemory address; relocatable inputs usually have no assigned address
24 / 0x188sh_offsetContent location relative to the file start
32 / 0x208sh_sizeContent size in bytes; for NOBITS, required memory size
40 / 0x284sh_linkAssociation with another section, interpreted according to section type
44 / 0x2c4sh_infoAdditional information, interpreted according to section type
48 / 0x308sh_addralignAddress alignment; zero and one impose no additional alignment
56 / 0x388sh_entsizeSize of each record in a fixed-record section

To locate entry i, calculate e_shoff + i × e_shentsize. Read the descriptor there, then use its sh_offset and sh_size to locate its payload. A NOBITS descriptor gives a memory requirement without supplying that many bytes in the file. Symbol and relocation tables also require following associations such as sh_link, as the next sections explain.

Section types determine how to decode the payload

sh_type, represented as Section.kind by a decoded section, is a category value. It is neither a section index nor a set of flags. The types needed for the current structures are:

ValueStandard nameMeaning
0SHT_NULLInactive section descriptor
1SHT_PROGBITSProgram-defined file contents, such as instructions or initialized data
2SHT_SYMTABRegular symbol table
3SHT_STRTABNUL-terminated name byte strings
4SHT_RELARelocations with explicit addends
8SHT_NOBITSMemory extent without corresponding file payload bytes
9SHT_RELRelocations whose addends come from the target contents
Section types and extensions used in later chapters
ValueStandard nameMeaning
5SHT_HASHSystem V symbol hash table
6SHT_DYNAMICDynamic-linking tag/value records
7SHT_NOTENamed, typed note records
10SHT_SHLIBReserved, unspecified semantics
11SHT_DYNSYMDynamic-linking symbol table
14 / 15 / 16SHT_INIT_ARRAY / SHT_FINI_ARRAY / SHT_PREINIT_ARRAYInitialization / termination / preinitialization function-pointer arrays
17SHT_GROUPSection group: flags followed by member section indices
18SHT_SYMTAB_SHNDXExtended section indices for symbols
19SHT_RELRPacked relative-relocation locations
0x70000001SHT_X86_64_UNWINDx86-64 unwind section type
0x6fff4c03SHT_LLVM_ADDRSIGLLVM address-significance table

0x60000000..0x6fffffff is OS-specific; 0x70000000..0x7fffffff is processor-specific. An unknown type does not by itself mean corruption; preservation and processing depend on implementation support.

Section flags combine independent attributes

ValueStandard nameMeaning
0x1SHF_WRITEWritable at runtime
0x2SHF_ALLOCPart of the runtime memory image
0x4SHF_EXECINSTRContains executable instructions
0x10SHF_MERGEEquivalent entries may be merged
0x20SHF_STRINGSMERGE entries are NUL-terminated strings
0x40SHF_INFO_LINKsh_info is a section-header index
0x80SHF_LINK_ORDEROrdering follows the linked section
0x100SHF_OS_NONCONFORMINGRequires OS-specific handling
0x200SHF_GROUPMember of a section group
0x400SHF_TLSPart of the thread-local storage template

sh_flags combines bits with OR and tests them with AND: 6 = ALLOC | EXECINSTR; 3 = WRITE | ALLOC; 0x12 = ALLOC | MERGE; 0x32 = ALLOC | MERGE | STRINGS; 0x403 = WRITE | ALLOC | TLS. flags == 6 requires an exact set; flags & SHF_ALLOC != 0 tests only the ALLOC bit. Absence of WRITE does not imply absence of ALLOC.

Relationship fields depend on the section type

Section typesh_linksh_info
SHT_SYMTAB / SHT_DYNSYMSection index of the name string tableSymbol index of the first non-LOCAL entry
SHT_RELA / SHT_RELSection index of the symbol tableTarget section index in ET_REL
SHT_GROUPSection index of the signature symbol tableSymbol index of the group signature

Section types, flags and relationships.

This Python reader explains a known-valid ELF64 little-endian input. It does not fully validate table bounds, extended numbering, string termination, or associated indices. A general-purpose parser must validate these conditions before performing the same lookups:

import struct, sys
data = open(sys.argv[1], "rb").read()
# ELF64: e_shoff at 0x28; e_shentsize/e_shnum/e_shstrndx at 0x3a
shoff = struct.unpack_from("<Q", data, 0x28)[0]
shentsize, shnum, shstrndx = struct.unpack_from("<HHH", data, 0x3a)
TYPES = {0: "NULL", 1: "PROGBITS", 2: "SYMTAB", 3: "STRTAB", 4: "RELA",
8: "NOBITS", 0x70000001: "X86_64_UNWIND", 0x6fff4c03: "LLVM_ADDRSIG"}
FLAGS = [(0x1, "W"), (0x2, "A"), (0x4, "X"), (0x10, "M"), (0x20, "S"), (0x40, "I")]
def shdr(i):
# sh_name, sh_type, sh_flags, sh_addr, sh_offset, sh_size,
# sh_link, sh_info, sh_addralign, sh_entsize
return struct.unpack_from("<IIQQQQIIQQ", data, shoff + i * shentsize)
names_off = shdr(shstrndx)[4]
def name(off):
end = data.index(b"\0", names_off + off)
return data[names_off + off:end].decode()
print("Nr Name Type Flg Off Size Lk Inf Al ES")
for i in range(shnum):
n, t, f, _, off, size, link, info, align, es = shdr(i)
flg = "".join(c for bit, c in FLAGS if f & bit)
print(f"{i:2} {name(n):16} {TYPES.get(t, hex(t)):13} {flg:3} "
f"{off:06x} {size:06x} {link:2} {info:3} {align:2} {es:2}")

The format <IIQQQQIIQQ selects little-endian integers with the widths required by the ELF64 structure. The result is:

$ python3 shdr.py main.o
Nr Name Type Flg Off Size Lk Inf Al ES
0 NULL 000000 000000 0 0 0 0
1 .strtab STRTAB 000238 0000a1 0 0 1 0
2 .text PROGBITS AX 000040 000015 0 0 16 0
3 .rela.text RELA I 0001d8 000030 14 2 8 24
4 .data PROGBITS WA 000058 000010 0 0 8 0
5 .rela.data RELA I 000208 000018 14 4 8 24
6 .rodata PROGBITS A 000070 000010 0 0 16 0
7 .rodata.str1.1 PROGBITS AMS 000080 000006 0 0 1 1
8 .bss NOBITS WA 000090 000100 0 0 16 0
9 .comment PROGBITS MS 000090 000028 0 0 1 1
10 .note.GNU-stack PROGBITS 0000b8 000000 0 0 1 0
11 .eh_frame X86_64_UNWIND A 0000b8 000030 0 0 8 0
12 .rela.eh_frame RELA I 000220 000018 14 11 8 24
13 .llvm_addrsig LLVM_ADDRSIG 000238 000000 14 0 1 0
14 .symtab SYMTAB 0000e8 0000f0 1 4 8 24

Types describe representation. PROGBITS stores ordinary contents; NOBITS reserves storage without contents; SYMTAB, STRTAB, and RELA identify structured tables. X86_64_UNWIND is a processor-specific type used for unwind information. LLVM_ADDRSIG is an LLVM extension supporting decisions about observable address identity, including safe ICF11.

Flags express other properties:

FlagValueMeaning
W0x1Writable
A0x2Allocated at runtime
X0x4Executable instructions
M0x10Mergeable elements
S0x20Elements are zero-terminated strings
I0x40sh_info is a section index

Thus .text is AX, .data and .bss are WA, and .rodata is A. Nonallocated tables serve tools rather than becoming ordinary mapped program storage.

.bss has size 0x100 for zeros[64], yet shares file offset 0x90 with the following .comment. No 256-byte file range is consumed by its storage. The assembler describes the reservation; the linker ultimately expresses the zero-filled runtime tail through segment sizes. With the toolchain's -fno-common behavior, this tentative C definition already belongs to .bss; common symbols are the next chapter's topic.

The relationships in sh_link and sh_info depend on section type. .rela.text links to symbol table 14 and targets section 2. .rela.data targets section 4. .rela.eh_frame targets section 11. .symtab links to string table 1; its info value 4 marks the first nonlocal symbol. Do not interpret all sh_info fields as section indices.

Section zero is SHT_NULL. It is all zero in this file, but large files may use it to hold extended numbering information. Likewise, the name .shstrtab is a convention rather than a requirement: LLVM here shares .strtab between section names and symbol names. Follow indices instead of guessing relationships from names.

$ llvm-objdump -s -j .strtab main.o
main.o: file format elf64-x86-64
Contents of section .strtab:
0000 002e7265 6c612e74 65787400 2e636f6d ..rela.text..com
0010 6d656e74 002e6273 73007a65 726f7300 ment..bss.zeros.
0020 636f756e 74657200 6d61696e 002e6e6f counter.main..no
0030 74652e47 4e552d73 7461636b 006d7367 te.GNU-stack.msg
0040 002e6c6c 766d5f61 64647273 6967002e ..llvm_addrsig..
0050 72656c61 2e65685f 6672616d 65007461 rela.eh_frame.ta
0060 626c6500 61646400 6d61696e 2e63002e ble.add.main.c..
0070 73747274 6162002e 73796d74 6162002e strtab..symtab..
0080 726f6461 7461002e 72656c61 2e646174 rodata..rela.dat
0090 61002e72 6f646174 612e7374 72312e31 a..rodata.str1.1
00a0 00 .

A string table is a byte sequence of zero-terminated names, referenced by byte offset. counter starts at 0x20, main at 0x28, and add at 0x64. A name may begin inside another stored string: .text starts at offset 6, inside .rela.text. This suffix sharing is legal because the index identifies bytes, not a numbered string record.

Match the stored bytes to the source

Generate main.s with clang -O1 -S main.c. Its data directives expose the intended organization:

.type counter,@object
.data
.globl counter
.p2align 2, 0x0
counter:
.long 42
.size counter, 4
.type table,@object
.section .rodata,"a",@progbits
.globl table
.p2align 4, 0x0
table:
.long 1
.long 2
.long 3
.long 4
.size table, 16
.type .L.str,@object
.section .rodata.str1.1,"aMS",@progbits,1
.L.str:
.asciz "hello"
.size .L.str, 6
.type msg,@object
.data
.globl msg
.p2align 3, 0x0
msg:
.quad .L.str
.size msg, 8
.type zeros,@object
.bss
.globl zeros
.p2align 4, 0x0
zeros:
.zero 256
.size zeros, 256

Returning to .data appends msg after counter and alignment padding. The generated code is:

$ llvm-objdump -d main.o
main.o: file format elf64-x86-64
Disassembly of section .text:
0000000000000000 <main>:
0: 50 pushq %rax
1: 8b 3d 00 00 00 00 movl (%rip), %edi # 0x7 <main+0x7>
7: be 03 00 00 00 movl $0x3, %esi
c: e8 00 00 00 00 callq 0x11 <main+0x11>
11: 01 c0 addl %eax, %eax
13: 59 popq %rcx
14: c3 retq

table[2] became immediate 3. The compiler knew the const array's value and eliminated the load. The push and pop maintain the call-site stack alignment; they are not preserving a meaningful C variable here. The two zero displacement fields still await linking.

$ llvm-objdump -s -j .data main.o
main.o: file format elf64-x86-64
Contents of section .data:
0000 2a000000 00000000 00000000 00000000 *...............

The first four data bytes encode 42, the next four are padding, and the final eight are the unresolved pointer msg. Declaring const char *msg makes the pointed-to characters const; it does not make the pointer object itself const.

The ordinary read-only section holds the four integers of table. The string has a separate mergeable-string input section with one-byte character units. The linker may combine identical strings, which is why references to pieces of mergeable sections require care.

$ llvm-objdump -s -j .bss main.o
[file header omitted]
Contents of section .bss:
<skipping contents of bss section at [0000, 0100)>

No .bss bytes are available to dump. The version string does have file contents but no allocation flag:

$ llvm-objdump -s -j .comment main.o
main.o: file format elf64-x86-64
Contents of section .comment:
0000 00556275 6e747520 636c616e 67207665 .Ubuntu clang ve
0010 7273696f 6e203231 2e312e38 20283675 rsion 21.1.8 (6u
0020 62756e74 75312900 buntu1).

.note.GNU-stack is another useful contrast: it has no contents and no allocation flag, yet communicates a requirement used in producing the executable. .eh_frame does have allocated contents and its own relocations, because unwind records must refer to the final code ranges.

A symbol record is more than a name

A symbol table contains descriptive records for functions, variables, and other symbols. It connects names to definitions, but the name strings and machine code normally live in other sections. A record must explain what a symbol is called, where it is defined, and what attributes it has. Start with the first two questions as concrete file lookups, then consider representation and linkage properties.

Three kinds of contents in one file

The diagram shows one illustrative 512-byte ELF64 relocatable file. Positions are decimal. Intervals include their start and exclude their end: [128,176) contains 48 bytes beginning at file offset 128.

One ELF file, its section descriptors, and the expanded symbol payload

The first four rows are successive bytes of the same file, not four files. The .text payload stores machine code, SYMTAB stores symbol records, and STRTAB stores name strings. The last two rows together form the section-header table. Each header describes a section’s type, payload location, and size. The header describing the symbol table is separate from the table’s contents: here the descriptor starts at file offset 384, while its payload starts at 128.

The ELF header supplies e_shoff=256, e_shentsize=64, and e_shnum=4: table origin, entry width, and entry count. Header 2 therefore begins at 256+2×64=384. Its sh_type identifies SYMTAB. A symbol table has no required section index; index 2 belongs only to this example layout.

Select one record from the table

The SYMTAB descriptor supplies sh_offset=128, sh_size=48, and sh_entsize=24: payload origin, total byte extent, and bytes per record. Its contents occupy [128,176) and contain 48÷24=2 records. Entry zero is the reserved all-zero symbol; entry one describes our example function. Indices start at zero, so entry one is the second record.

Its file origin is 128+1×24=152, and its bytes occupy [152,176). The multiplication converts a record index into a byte offset: 1 record times 24 bytes per record. This locates the descriptor of a symbol, not yet its name or its machine code.

Decode fields using the record format

The ELF symbol-table specification fixes the boundaries of Elf64_Sym. st_name occupies four bytes, st_info and st_other one each, st_shndx two, and st_value and st_size eight each: 24 bytes in total. Labels such as +0 and +4 start at this record, not at the file. Widths below the boxes describe storage, not stored values.

The 24-byte symbol format with record-relative offsets and example values

Our example uses little-endian encoding: 01 00 00 00 represents integer 1. Splitting the bytes at [152,176) yields st_name=1, st_info=0x12, st_other=0, st_shndx=1, st_value=0, and st_size=21. Origin 152 comes from the file layout; field widths come from the format; values 1, 0, and 21 come from the bytes. Those are different sources of information.

Numeric values of symbol attributes

The upper four bits of st_info encode binding; the lower four encode type. Decode them as binding = st_info >> 4 and type = st_info & 0x0f. They are separate categories, not one type byte.

Binding

ValueStandard nameMeaning
0STB_LOCALLocal to its object file
1STB_GLOBALMay satisfy references from other objects
2STB_WEAKWeak binding; selection rules appear in Theory 03
10STB_GNU_UNIQUEGNU extension for process-wide unique binding

Type

ValueStandard nameMeaning
0STT_NOTYPENo specific entity category supplied
1STT_OBJECTData object
2STT_FUNCFunction
3STT_SECTIONSection symbol, often used by relocations
4STT_FILESource-file identifier, not a runtime object
5STT_COMMONCommon data category; distinct from the SHN_COMMON field value
6STT_TLSThread-local object, with a distinct value coordinate
10STT_GNU_IFUNCGNU indirect function whose implementation is selected by a resolver

Symbol.kind=2 means FUNC; Section.kind=2 means SYMTAB. The containing field determines the meaning. Thus st_info=0x12 is a GLOBAL function and 0x21 is a WEAK data object.

Visibility

Visibility is st_other & 0x03; the remaining bits are not binding or type.

ValueStandard nameMeaning
0STV_DEFAULTDefault binding and visibility rules
1STV_INTERNALInternal visibility with processor-specific constraints
2STV_HIDDENName unavailable for external lookup by other modules
3STV_PROTECTEDExternally visible; references within the defining module bind locally

st_shndx

ValueStandard nameMeaning
0SHN_UNDEFUndefined in this file, not defined in section zero
1..0xfeff普通节下标 / ordinary indexMust also be below the section count; value is section-relative in ET_REL
0xff00SHN_LORESERVEStart of the reserved 16-bit section-index range
0xfff1SHN_ABSAbsolute value, independent of ordinary section placement
0xfff2SHN_COMMONUnallocated storage; value is alignment and size is the extent
0xffffSHN_XINDEXReal index is in the associated SHT_SYMTAB_SHNDX table

The reserved range also contains OS/processor-specific values. Distinguish supported special values, valid ordinary indices, and unsupported reserved values rather than indexing with every nonzero value. LOCAL/GLOBAL/WEAK describe binding; UNDEF/ABS/COMMON describe definition placement. Neither classification replaces the other.

Symbol attribute definitions.

Follow separate paths to name and code

Two lookups from one symbol: its string-table name and its text-section code

For the name, sh_link=3 in the symbol-table header selects section-header entry 3. That header describes the string table and supplies sh_offset=176. The symbol’s st_name=1 starts one byte into its contents, hence file offset 176+1=177. Bytes 6d 61 69 6e 00 encode main followed by a terminating NUL. NUL is not part of the name. sh_link chooses a table; st_name chooses a position inside that table.

For the code, st_shndx=1 selects section-header entry 1, describing .text. Its payload starts at sh_offset=64. For this ordinary ET_REL function definition, st_value=0 is a byte offset within that section, and st_size=21 is the function’s size. Its input code occupies [64+0,64+0+21), or [64,85). The symbol record describes that region; it does not contain the code itself. This is still an input-file location, not the function’s eventual runtime address.

Three quantities happen to equal one: the symbol-record index, st_name, and st_shndx. They select a symbol record, a byte in a string table, and a section header respectively. Equal numbers do not imply equal objects or units.

Definition categories and symbol attributes

Not every symbol defines a function in an ordinary section. SHN_UNDEF means the file does not define the symbol; SHN_ABS denotes an absolute symbol; SHN_COMMON describes COMMON storage awaiting allocation. An ordinary ET_REL section definition uses st_value as a section-relative position. An ABS value is independent of an ordinary section’s placement; COMMON uses the value to express alignment. Interpret the definition category before interpreting its value. Larger section indices use the format’s extended representation rather than an ordinary 16-bit section index.

st_info packs binding in its high four bits and type in its low four. Here 0x12 splits into binding 1, GLOBAL, and type 2, FUNC. LOCAL names participate in matching within their object file; GLOBAL definitions can satisfy references from other files; WEAK definitions have lower precedence than ordinary GLOBAL definitions. Types distinguish FUNC, OBJECT, SECTION, FILE, TLS, and other entities. st_other also carries visibility, which affects cross-shared-object visibility and binding. These attributes guide later linkage decisions without changing the record-location formula.

LOCAL records precede non-LOCAL records. The SYMTAB header’s sh_info gives the first non-LOCAL symbol index. Here it is 1, selecting the GLOBAL function after the reserved zero entry. That sh_info is a symbol index, not a section index. The section type determines how to interpret sh_link and sh_info; their meaning cannot be transferred unchanged to every section header.

Binding and type values 10–12 are reserved for OS extensions, and 13–15 for processor extensions. GNU UNIQUE and IFUNC are particular extensions, not universally supported generic categories.

A symbol index is local to its owning symbol table. Different object files—and different symbol tables in one file—can each have an entry one. Names are resolved through the associated string table and definitions through their sections. Once those relationships are established, the linker can match names across files and assign output addresses.

A relocation is a request, not a search for zero bytes

A symbol record describes an entity and its definition. It does not identify every instruction or data field that needs that entity’s address. A relocation supplies the missing relation: a destination, a symbol, and a method for calculating and encoding the result. The record’s own location, its destination, and the referenced definition are three different locations.

Establish the record’s ownership first

In a relocatable file, the RELA section header establishes three relationships. sh_offset/sh_size/sh_entsize locate and divide the record table itself; sh_info selects the target section by section-header index; sh_link selects the symbol table by section-header index. A record does not normally write back into its own table, and its symbol index cannot be used as a section index.

Suppose the target .text payload starts at file offset 64. A record with r_offset=3 selects a field three bytes into that payload, beginning at file offset 64+3=67. This locates input bytes only. The value to write also requires symbol resolution and output placement.

A 24-byte record describes one modification

RELA fields and destination bytes: widths, symbol ownership, and the write location

ELF64 RELA consists of three eight-byte fields. r_offset is the target-section byte offset; r_info packs a symbol index and relocation type; r_addend stores a signed addend. Diagram offsets +0/+8/+16 begin at the record, not at destination file offset 67.

For r_info=0x0000000100000002, the high 32 bits are symbol index 1 and the low 32 bits are type 2. Select symbol record one in the table owned by the RELA header’s sh_link, then apply the type’s rules. Little-endian bytes are 02 00 00 00 01 00 00 00: numerically high bits reside in the later bytes. Numeric significance and file-byte order are different concepts.

The processor ABI assigns meanings to type numbers. On x86-64, type 2 is R_X86_64_PC32, which writes a signed 32-bit result. This example therefore modifies four bytes at file [67,71), although the record describing that operation occupies 24 bytes. Record size does not determine write width. Signed addend −4 has i64 little-endian bytes fc ff ff ff ff ff ff ff; the relocation chapter derives its role in PC-relative calculations.

Ownership and coordinates depend on context

The symbol index in r_info belongs to the selected symbol table. Resolving its name and definition still follows the st_name and definition-category relationships introduced above. It is not an identifier shared across object files. An unknown type cannot safely be treated as an ordinary address write: types may use different expressions, field widths, or scattered instruction bits.

The section-relative meaning of r_offset applies to ET_REL. In executable or shared-object dynamic relocations it is a link-time virtual address of the destination; the loader uses the load bias to obtain a runtime address. The input-file formula cannot be reused unchanged. ELF also defines REL without an explicit r_addend: its ELF64 record occupies 16 bytes and the target field supplies an implicit addend, extracted as the processor ABI specifies. RELA’s explicit addend and REL’s implicit addend are different representations.

Decoding and application are consequently separate stages. Decoding recovers the fields and their owning tables while preserving symbol indices, types, and signed addends. Application combines the selected definition, output placement, and type-specific rules to calculate, range-check, and write a result.

Linking adds the loader's view

Sections provide the detailed units used in construction. Segments describe how to establish the runtime image. To see both views in one file, provide a minimal entry point without a C runtime:

# start.S: entry without a C runtime
.text
.globl _start
_start:
call main
movl %eax, %edi # use the return value of main as the exit status
movl $60, %eax # 60 is the x86-64 Linux exit syscall number
syscall
.section .note.GNU-stack,"",@progbits

Link the three objects and inspect the result:

$ clang -c start.S -o start.o
$ ld.lld -o prog start.o main.o add.o
$ readelf -lW prog
Elf file type is EXEC (Executable file)
Entry point 0x2011d0
There are 5 program headers, starting at offset 64
Program Headers:
Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
PHDR 0x000040 0x0000000000200040 0x0000000000200040 0x000118 0x000118 R 0x8
LOAD 0x000000 0x0000000000200000 0x0000000000200000 0x0001c4 0x0001c4 R 0x1000
LOAD 0x0001d0 0x00000000002011d0 0x00000000002011d0 0x000034 0x000034 R E 0x1000
LOAD 0x000208 0x0000000000202208 0x0000000000202208 0x000010 0x000118 RW 0x1000
GNU_STACK 0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 RW 0
Section to Segment mapping:
Segment Sections...
00
01 .rodata .eh_frame
02 .text
03 .data .bss
04

Each ELF64 program header is a 56-byte Elf64_Phdr. Offsets below are relative to the start of the current entry; widths describe the fields themselves:

Entry offsetWidthFieldMeaning
04p_typeThe program header's role
44p_flagsPermission bits: X=1, W=2, R=4
88p_offsetPayload offset from the beginning of the file
168p_vaddrSegment virtual address
248p_paddrPhysical address; unused for ordinary user programs
328p_fileszPayload size in the file
408p_memszPayload size in memory
488p_alignAlignment requirement

The p_filesz field always occupies eight bytes. A value of 16 describes sixteen payload bytes; field width and represented size are different quantities. Entry i starts at e_phoff + i × e_phentsize. Its p_offset points to content elsewhere in the file: the entry itself is not that content. Segment permissions differ from section SHF_* flags: RX is 5, while a code section's ALLOC|EXECINSTR is 6.

For PT_LOAD, file offset and virtual address describe the mapping, file and memory sizes distinguish stored bytes from total storage, and alignment constrains their relationship. PT_PHDR describes the program header table itself, while PT_GNU_STACK communicates stack permissions without describing a mapped file payload.

This output groups read-only constants and unwind data in an R segment, code in an RE segment, and writable data plus zero-filled storage in an RW segment. The RW segment has file size 0x10 but memory size 0x118. The latter includes the gap before the aligned .bss and its 256 bytes: .bss ends at 0x202320, and subtracting the segment base 0x202208 gives 0x118.

The input relocations have been applied and their tables are absent from this particular output. That is not a rule that executables never contain relocations: dynamic linking and --emit-relocs provide obvious counterexamples. The loader needs the program headers to establish the image; it does not need the detailed input-section history.

We can now identify an object file's unfinished work directly. The next chapter examines the first decision needed to finish it: choosing definitions when several inputs offer, request, or weakly reference the same name.

Optional detour: compact integers and assembly preprocessing

The main ELF walkthrough is complete. Return to hand.S here to inspect its variable-length integers and preprocessing; neither is required to follow the first header, symbol, or relocation record.

Small integers deserve small encodings

Fixed-width .long uses four bytes whether the value is 3 or a million. Metadata often contains many small integers and only occasional large ones. LEB12812 uses seven payload bits per byte, with bit 7 indicating whether another byte follows. Low-order groups come first.

For unsigned 624485:

  1. The low seven bits are 0x65; more remain, so emit 0xe5.
  2. The next group is 0x0e; more remain, so emit 0x8e.
  3. The final group is 0x26; emit it without the continuation bit.

The result is e5 8e 26. Signed LEB128 stops when the remaining high bits are all sign bits and bit 6 of the final payload agrees with that sign. For −123456, the groups produce c0 bb 78; the decoder sign-extends from the final group's bit 6. Merely reaching a remaining value of −1 is not sufficient unless the emitted group's sign bit agrees.

DWARF 5, section 7.6 specifies these encodings. They also appear in WebAssembly and DEX, so recognizing them pays off beyond ELF.

Inspect the assembled read-only data:

$ clang -c hand.S -o hand-small.o
$ llvm-objdump -s -j .rodata hand-small.o
hand-small.o: file format elf64-x86-64
Contents of section .rodata:
0000 686900ff 00000000 e58e26c0 bb786400 hi........&..xd.
0010 0000 ..

Offsets 0–2 hold hi and a zero terminator. Offset 3 holds ff. Four padding bytes advance the next value to offset 8. The two LEB128 values occupy three bytes each, and the macro's integer begins at offset 0xe. Its 64 00 00 00 is the little-endian encoding of 100. Total size: 0x12 bytes.

Uppercase .S requests preprocessing

The #ifdef belongs to the C preprocessor, not the assembler's macro system. Driver conventions normally preprocess .S before assembly; lowercase .s normally skips that step. Define BIG to select the other macro invocation:

$ clang -DBIG -c hand.S -o hand-big.o
$ llvm-objdump -s -j .rodata hand-big.o
hand-big.o: file format elf64-x86-64
Contents of section .rodata:
0000 686900ff 00000000 e58e26c0 bb78a086 hi........&..x..
0010 0100 ..

The final integer changes to a0 86 01 00, or 100000. Now deliberately use the lowercase suffix:

$ cp hand.S plain.s
$ clang -DBIG -c plain.s -o plain.o
clang: warning: argument unused during compilation: '-D BIG' [-Wunused-command-line-argument]
<instantiation>:2:1: error: symbol 'limit' is already defined
limit:
^

The unused-option warning tells us preprocessing did not occur. In this x86 assembly syntax, lines beginning with # are comments, so both CONST invocations remain and both define limit. Explicitly selecting the language restores preprocessing:

$ clang -x assembler-with-cpp -DBIG -c plain.s -o plain.o
$ llvm-objdump -s -j .rodata plain.o | tail -2
0000 686900ff 00000000 e58e26c0 bb78a086 hi........&..x..
0010 0100 ..

Kernel and runtime assembly often uses .S to share constants and select architecture features. Whatever the source-level machinery, the linker receives the resulting object file.

Exercises

1. Predict the symbol table

Compile with clang -O0 -c quiz.c, but classify the source names before inspecting the object:

// quiz.c
int printf(const char *fmt, ...);
extern int limit;
extern int unused;
int total = 0;
__attribute__((weak)) int verbose = 1;
static int square(int v) { return v * v; }
int report(int n) {
static int calls;
int sum = square(n) + total + limit;
if (verbose) printf("%d: %d\n", ++calls, sum);
return sum;
}

For printf, limit, unused, total, verbose, square, v, report, n, calls, sum, and the string literal, predict whether an ELF symbol appears. For each that does, predict binding, type, and definition section or undefined status. Check with readelf -sW quiz.o, then repeat at -O1.

2. Predict every fixup

# predict.s
.text
.globl entry
.type entry,@function
entry:
movl counter(%rip), %eax
call bump
call ext_func
leaq .Lmsg(%rip), %rdi
jmp .Lout
.Lout:
ret
.size entry, . - entry
.section .text.cold,"ax",@progbits
bump:
ret
.data
.globl counter
counter:
.long 7
.long .Lout - entry
slot:
.quad counter + 8
.quad slot
.section .rodata.str1.1,"aMS",@progbits,1
.Lmsg:
.asciz "ok"
.section .note.GNU-stack,"",@progbits

List the sections, symbols, and relocation records before assembling. For each relocation give the target section, field offset, type, symbol, and addend. Which expressions can the assembler finish? The RIP-relative movl prefix occupies two bytes, call one, and the leaq prefix three. Assemble with Clang and GNU as; identify the relocation type that differs.

3. Change permissions without changing instructions

.section .boot,"ax",@progbits
.globl _start
_start:
movl $60, %eax
movl $42, %edi
syscall
.section .note.GNU-stack,"",@progbits

Assemble and link with LLD and GNU ld, then run natively:

$ ./prog; echo $?

After observing 42 with "ax", try "a" and "x". Predict linker diagnostics, program headers, entry address, and runtime behavior. Restore "ax" afterward.

Answers and recorded results

1. Source names and linker symbols are different sets

$ clang -O0 -c quiz.c -o quiz.o
$ readelf -sW quiz.o
Symbol table '.symtab' contains 12 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 0000000000000000 0 FILE LOCAL DEFAULT ABS quiz.c
2: 0000000000000000 0 SECTION LOCAL DEFAULT 2 .text
3: 0000000000000060 16 FUNC LOCAL DEFAULT 2 square
4: 0000000000000004 4 OBJECT LOCAL DEFAULT 4 report.calls
5: 0000000000000000 8 OBJECT LOCAL DEFAULT 6 .L.str
6: 0000000000000000 0 SECTION LOCAL DEFAULT 4 .bss
7: 0000000000000000 87 FUNC GLOBAL DEFAULT 2 report
8: 0000000000000000 4 OBJECT GLOBAL DEFAULT 4 total
9: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND limit
10: 0000000000000000 4 OBJECT WEAK DEFAULT 5 verbose
11: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND printf

printf and limit are global undefined symbols. unused is only a declaration and produces no reference. total is a global object in .bss, despite explicitly spelling its zero initializer. verbose is a weak object in .data. square is a local function; report a global function. The function-local static becomes local object report.calls in .bss; unlike an automatic variable, it has persistent storage.

Parameters v and n, and automatic sum, do not become ordinary linker symbols. Their register or stack locations belong to individual calls and, when available, debug descriptions. “Local symbol” and “local C variable” describe different concepts.

The string appears here as local .L.str. The PC-relative reference has addend −4 and targets a mergeable-string section. Replacing that reference naively with a section symbol plus an offset can lose the association with the correct string when pieces are merged. The assembler retains the label. The earlier absolute reference with zero addend could be represented through the section symbol. GCC's placement into ordinary .rodata produces a different representation again. Predict from the actual relocation and section properties, not from a blanket rule that all .L names disappear.

At -O1, square is inlined and its standalone symbol disappears in the recorded result.

2. Six relocations, two resolved expressions

$ clang -c predict.s -o predict.o
$ readelf -sW predict.o
Symbol table '.symtab' contains 9 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 0000000000000000 0 NOTYPE LOCAL DEFAULT 4 bump
2: 0000000000000000 0 NOTYPE LOCAL DEFAULT 7 .Lmsg
3: 0000000000000000 0 SECTION LOCAL DEFAULT 4 .text.cold
4: 0000000000000000 0 SECTION LOCAL DEFAULT 5 .data
5: 0000000000000008 0 NOTYPE LOCAL DEFAULT 5 slot
6: 0000000000000000 26 FUNC GLOBAL DEFAULT 2 entry
7: 0000000000000000 0 NOTYPE GLOBAL DEFAULT 5 counter
8: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND ext_func
$ llvm-objdump -dr predict.o
[file header omitted]
0000000000000000 <entry>:
0: 8b 05 00 00 00 00 movl (%rip), %eax # 0x6 <entry+0x6>
0000000000000002: R_X86_64_PC32 counter-0x4
6: e8 00 00 00 00 callq 0xb <entry+0xb>
0000000000000007: R_X86_64_PLT32 .text.cold-0x4
b: e8 00 00 00 00 callq 0x10 <entry+0x10>
000000000000000c: R_X86_64_PLT32 ext_func-0x4
10: 48 8d 3d 00 00 00 00 leaq (%rip), %rdi # 0x17 <entry+0x17>
0000000000000013: R_X86_64_PC32 .Lmsg-0x4
17: eb 00 jmp 0x19 <entry+0x19>
19: c3 retq
[excerpt]
$ llvm-objdump -r predict.o | tail -4
RELOCATION RECORDS FOR [.data]:
OFFSET TYPE VALUE
0000000000000008 R_X86_64_64 counter+0x8
0000000000000010 R_X86_64_64 .data+0x8
$ llvm-objdump -s -j .data predict.o | tail -2
0000 07000000 19000000 00000000 00000000 ................
0010 00000000 00000000 ........

The content sections are .text (0x1a bytes), .text.cold (one byte), .data (0x18), .rodata.str1.1 (three), and the empty stack note. Symbol, string, and relocation tables accompany them. Additional empty or property sections vary with the assembler and its configuration.

entry is a global function of size 26. counter is global but has no explicit type or size. ext_func is undefined. bump and slot are local. .Lmsg is retained for the mergeable-section reference; .Lout is unnecessary as a stored symbol. Section symbols describe local relocation targets.

Target fieldTypeReferenced location
.text+0x2PC32counter - 4
.text+0x7PLT32 in LLVM output.text.cold - 4
.text+0xcPLT32ext_func - 4
.text+0x13PC32.Lmsg - 4
.data+0x864counter + 8
.data+0x1064.data + 8

The short jump to the following instruction is already eb 00. The same-section difference .Lout - entry is 0x19, stored at .data+4 as 19 00 00 00.

GNU as uses PC32 for the local cross-section call:

$ as predict.s -o predict-gas.o
$ llvm-objdump -dr predict-gas.o | grep -A1 'callq'
6: e8 00 00 00 00 callq 0xb <entry+0xb>
0000000000000007: R_X86_64_PC32 .text.cold-0x4
--
b: e8 00 00 00 00 callq 0x10 <entry+0x10>
000000000000000c: R_X86_64_PLT32 ext_func-0x4

Because that local target cannot bind through a shared-library PLT, the two recorded relocation types lead to the same displacement in this case. Their general semantics still differ.

3. Successful linking does not prove executable permissions

$ ld.lld -o lld-ax flag-ax.o; ld -o gnu-ax flag-ax.o
$ readelf -lW lld-ax | grep -E 'Entry|LOAD'
Entry point 0x201120
LOAD 0x000000 0x0000000000200000 0x0000000000200000 0x000120 0x000120 R 0x1000
LOAD 0x000120 0x0000000000201120 0x0000000000201120 0x00000c 0x00000c R E 0x1000
$ readelf -lW lld-a | grep -E 'Entry|LOAD'
Entry point 0x200120
LOAD 0x000000 0x0000000000200000 0x0000000000200000 0x00012c 0x00012c R 0x1000
$ readelf -lW lld-x | grep -E 'Entry|LOAD'
Entry point 0x0
LOAD 0x000000 0x0000000000200000 0x0000000000200000 0x000120 0x000120 R 0x1000
$ readelf -lW gnu-x | grep -E 'Entry|LOAD'
Entry point 0x0

With "ax", both linkers place .boot in an executable load segment and both native executions return 42. With "a", both links succeed without warning, but the entry is in a read-only, non-executable mapping. Execution fails with SIGSEGV; the recorded shell status is 139.

With "x", the section lacks allocation. It is absent from PT_LOAD, and the recorded entry address is zero. LLD retains a header-only read-only load segment; GNU ld emits no load segment. Both recorded native executions fail with SIGSEGV and shell status 139. The exact entry and layout are observations of these versions, while the underlying mistake is the missing allocation requirement.

A linker organizes sections according to their declared properties. It does not infer that bytes resembling instructions must be executable. The object file is a contract, including the parts its author got wrong.

References and terminology

The ELF section specification, symbol-table specification, GNU assembler manual, LLVM toolchain description, and DWARF 5 standard define the structures and encodings used here. The symbol-classification exercise follows the teaching pattern used in CMU 15-213; Princeton COS 217's assembler/linker material offers a complementary introduction to fixups.

Appendix: terms and tools

  1. ELF, the Executable and Linkable Format, specifies object files, executables, and shared objects. The gABI supplies generic rules; a processor-specific ABI supplies architecture-dependent rules such as relocation encodings. ELF specification. ↩

  2. cc is the conventional C compiler command. It may select GCC or Clang; inspect cc --version or name the implementation explicitly for reproducible experiments. Toolchain overview. ↩

  3. IR means intermediate representation. LLVM IR describes computation between source translation and machine-code generation; its textual and bitcode forms represent the same intermediate language. LLVM language reference. ↩

  4. LLVM names a collection of compiler and toolchain projects, including optimization and code-generation infrastructure. Clang, LLD, and LLVM IR are related but have different roles. LLVM. ↩

  5. GCC, the GNU Compiler Collection, provides compilers for several languages. The gcc command is a driver that coordinates compilation, assembly, and linking; it need not perform all those operations in one process. Overall options. ↩

  6. GNU as encodes assembly into object files. Instructions describe machine operations, while directives such as .section and .globl organize sections and symbols. Assembler manual. ↩

  7. readelf inspects ELF headers, sections, segments, symbols, and relocations without executing the input program. GNU and LLVM variants need not format their output identically. Manual. ↩

  8. nm lists symbols. Its letter codes summarize attributes such as section and binding; inspect the ELF symbol fields when the precise semantics matter. Manual. ↩

  9. psABI, processor-specific ABI, defines the binary contract for one architecture. Architectures can share ELF containers while differing in instruction encodings, calling conventions, and relocations. RISC-V psABI. ↩

  10. PIE, a position-independent executable, can run at different load bases. Compiler and linker choices must cooperate; static PIE also needs a startup path that performs its required relocations. GCC link options. ↩

  11. ICF, Identical Code Folding, merges code judged equivalent. Matching bytes alone may be insufficient: relocation targets, observable function addresses, and associated runtime metadata also matter. LLD. ↩

  12. LEB128 means Little Endian Base 128. It encodes seven payload bits per byte and has distinct signed and unsigned forms. DWARF specification. ↩