Question Details

No question body available.

Tags

c linker elf

Answers (1)

August 17, 2025 Score: 2 Rep: 152,768 Quality: Medium Completeness: 60%

Both order and andthe are in the .data segment because they are modifiable data items, an int and a char pointer.

Note that if you declared andthe as const char and_the = "DDD";, it would still be a modifiable pointer, but to const char, so this would not change anything.

If you declared and_the as char const andthe = "DDD"; or const char * const andthe = "DDD"; then andthe would be located in a read only data segment, .rodata or even a code segment on some architectures.

Regarding the relocations, the initial value of order itself does not need relocating because it is just an integer constant 5, displayed as 05 00 00 00 is the data segment dump (which it makes no sense to disassemble).

The code that uses order needs relocating: if you disassemble main in the object module (before linking), you should see an instruction to access order and pass it to printf, with an offset of 00 00 00 00 that will be relocated at load time by adding the offset of the .data segment of the module.

Similarly, The code that uses andthe need relocating: if you disassemble main in the object module before linking, you should see an instruction to access andthe and pass it to printf, with an offset of 08 00 00 00 (because andthe is aligned on 8 bytes and located after order) that will be relocated at load time by adding the offset of the .data segment of the object module.

The linker will patch these locations by adding the final offset in the .data segment of the program (the data segments of all object modules linked are concatenated).

The initial value of end_the also need relocating: in the object module it has the offset of the string "DDD" is the module's read only segment, where the other strings "%d\n" and "%s\n" are also located, and this offset is patched at load time by adding the offset in memory of the module's read only segment.

Depending on the architecture, this data segment may be loaded at an absolute address in virtual memory, so no further relocations are needed for the references to data located there, but modern architectures don't do this for various reasons (dynamic libraries are loaded at available addresses, not fixed ones, and address space randomisation is used to complicate exploitation of software flaws). If the .data segment is finally loaded in memory at a different address than the one pre-selected by the linker, all offsets in code and data in this segment need relocating by adding the difference of offsets between the linker's pre-set and the actual address.

All this work is performed by the program loader at start time, or in the page mapper whenever a page fault occurs at run time for a page that has not been mapped yet into the process address space.