<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Research on pugachev.io</title><link>https://pugachev.io/tags/research/</link><description>Recent content in Research on pugachev.io</description><generator>Hugo</generator><language>en-US</language><copyright>© Maxim Pugachev</copyright><lastBuildDate>Wed, 02 Dec 2026 11:27:16 +0100</lastBuildDate><atom:link href="https://pugachev.io/tags/research/index.xml" rel="self" type="application/rss+xml"/><item><title>GOT, PLT, RELRO, and other random characters</title><link>https://pugachev.io/2026/11/08/got-plt-relro-and-other-random-characters/</link><pubDate>Sun, 08 Nov 2026 11:16:29 +0100</pubDate><guid>https://pugachev.io/2026/11/08/got-plt-relro-and-other-random-characters/</guid><description>&lt;p&gt;Every time I open a dynamically linked ELF in a disassembler, I see the same bunch of &lt;code&gt;puts@plt&lt;/code&gt;, &lt;code&gt;_GLOBAL_OFFSET_TABLE_+0x18&lt;/code&gt;, &lt;code&gt;.plt.sec&lt;/code&gt;, &lt;code&gt;.got&lt;/code&gt; and &lt;code&gt;.got.plt&lt;/code&gt;. I usually trust IDA (until it breaks), but sometimes this blind faith, and the lack of time to understand the details behind it, makes you feel uncomfortable. So I decided to finally figure out how, say, a &lt;code&gt;puts(3)&lt;/code&gt; call in my awesome 3-liner in C finds the right &lt;code&gt;puts&lt;/code&gt; in glibc, when nobody in the toolchain knew its address at compile time. And what all these random characters around it are for. And all those sections. And ASLR with CET on top. Well, quite a lot of questions to answer.&lt;/p&gt;&#10;&lt;p&gt;Everything here is about x86-64 Linux (System V AMD64 ABI, to be more precise) and glibc. I built and ran every binary on my Debian 13.8 with gcc 14.2, binutils 2.44 and glibc 2.41, so I didn't make up any of the &lt;code&gt;objdump&lt;/code&gt;, &lt;code&gt;readelf&lt;/code&gt; and &lt;code&gt;gdb&lt;/code&gt; output below. The links to the glibc sources point to the &lt;code&gt;glibc-2.41&lt;/code&gt; tag, so the line numbers (should still) match that release. On a different toolchain the mechanics are the same (at least I believe so), only the addresses and counts like &lt;code&gt;RELACOUNT&lt;/code&gt; will be different.&lt;/p&gt;&#10;&lt;h2 id="why-any-of-this-is-needed"&gt;Why any of this is needed&lt;/h2&gt;&#10;&lt;p&gt;But let's start from the beginning. You suddenly call &lt;code&gt;puts(&amp;quot;hi&amp;quot;)&lt;/code&gt;, and this &lt;code&gt;puts&lt;/code&gt; lives inside &lt;code&gt;libc.so.6&lt;/code&gt;, which is a separate file. The loader maps it somewhere into your process, and thanks to the kernel's ASLR this &amp;quot;somewhere&amp;quot; is different every time you run the program. In other words, neither the compiler nor the linker can know the address of &lt;code&gt;puts&lt;/code&gt; in advance, so the code can't just do &lt;code&gt;call 0x7ffff1337101&lt;/code&gt;: this address doesn't exist yet, and it won't be the same next time anyway.&lt;/p&gt;&#10;&lt;p&gt;So there are two ways out of this. Either you patch the real address into every call site once you know it, or you route all calls to &lt;code&gt;puts&lt;/code&gt; through one pointer in writable memory and patch only that pointer once. The second option is what the toolchain actually does, but it's worth understanding why the first one is so bad, and &amp;quot;man, it's slow!&amp;quot; isn't even the main reason.&lt;/p&gt;&#10;&lt;h3 id="why-you-cant-just-patch-the-text"&gt;Why you can't just patch the &lt;code&gt;.text&lt;/code&gt;&lt;/h3&gt;&#10;&lt;p&gt;Say the linker left &lt;code&gt;call 0x0&lt;/code&gt; placeholders in &lt;code&gt;.text&lt;/code&gt;, and the loader fixed them up with the real address of &lt;code&gt;puts&lt;/code&gt; at load time. Four things break at once:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;It dirties code pages. &lt;code&gt;.text&lt;/code&gt; is mapped as &lt;code&gt;r-xp&lt;/code&gt;: read, execute, no write, and the strange trailing &lt;code&gt;p&lt;/code&gt;, which isn't a permission but the sharing mode in &lt;a href="https://man7.org/linux/man-pages/man5/proc_pid_maps.5.html" target="_blank" rel="noopener"&gt;&lt;code&gt;/proc/[pid]/maps&lt;/code&gt;&lt;/a&gt; (why not, right?). This &lt;code&gt;p&lt;/code&gt; means private, or copy-on-write, and &lt;code&gt;s&lt;/code&gt; means shared, just like &lt;code&gt;MAP_PRIVATE&lt;/code&gt; and &lt;code&gt;MAP_SHARED&lt;/code&gt; in &lt;code&gt;mmap(2)&lt;/code&gt;. The &lt;code&gt;r-x&lt;/code&gt; part comes from the smart linker, which puts &lt;code&gt;.text&lt;/code&gt; into a &lt;code&gt;PT_LOAD&lt;/code&gt; segment with &lt;code&gt;R E&lt;/code&gt; flags (&lt;code&gt;p_flags = PF_R | PF_X&lt;/code&gt;, the &lt;code&gt;Flg&lt;/code&gt; column in &lt;code&gt;readelf -l&lt;/code&gt;). And the &lt;code&gt;p&lt;/code&gt; part comes from the kernel: &lt;a href="https://github.com/torvalds/linux/blob/v6.12/fs/binfmt_elf.c#L1035" target="_blank" rel="noopener"&gt;&lt;code&gt;load_elf_binary()&lt;/code&gt;&lt;/a&gt; walks every &lt;code&gt;PT_LOAD&lt;/code&gt;, turns its flags into page protections with &lt;a href="https://github.com/torvalds/linux/blob/v6.12/fs/binfmt_elf.c#L611-L624" target="_blank" rel="noopener"&gt;&lt;code&gt;make_prot()&lt;/code&gt;&lt;/a&gt; and maps it as &lt;a href="https://github.com/torvalds/linux/blob/v6.12/fs/binfmt_elf.c#L1041" target="_blank" rel="noopener"&gt;&lt;code&gt;MAP_PRIVATE&lt;/code&gt;&lt;/a&gt;. Such pages are then shared between all processes running the same binary, until one of them writes to a page for some reason. In that case, the kernel has to make a private copy of that page for this sneaky process only. Try patching thousands of call sites in a real program, and you end up with most of its code turned into dirty memory unique to this process. The kernel can no longer drop these pages and read them back from the file when needed, and the whole point of sharing one physical copy of libc's code between all processes is gone.&lt;/li&gt;&#10;&lt;li&gt;It needs writable code. To patch &lt;code&gt;.text&lt;/code&gt;, the loader has to make it writable, at least for a while. glibc does just that for binaries with old-school text relocations: it &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-reloc.c#L281" target="_blank" rel="noopener"&gt;adds &lt;code&gt;PROT_WRITE&lt;/code&gt;&lt;/a&gt; to the segment's &lt;code&gt;r-x&lt;/code&gt;, so for a moment the code is both writable and executable. And this is what &lt;a href="https://en.wikipedia.org/wiki/W%5EX" target="_blank" rel="noopener"&gt;W^X&lt;/a&gt; (write xor execute) exists to prevent, for buggy programs and shellcode kiddies alike. Old-school doesn't mean forgotten, though: such text relocations still happen in the wild. More on that just below.&lt;/li&gt;&#10;&lt;li&gt;The relocations multiply. Every call site of &lt;code&gt;puts&lt;/code&gt; needs its own fixup, so if you call &lt;code&gt;puts&lt;/code&gt; from forty places, that's forty addresses to patch. Route all of them through one pointer, and it's just one.&lt;/li&gt;&#10;&lt;li&gt;It doesn't even fit. A direct &lt;code&gt;call&lt;/code&gt; on x86-64 takes a signed 32-bit offset, so it can reach only 2 GB in either direction. A PIE usually lives somewhere around &lt;code&gt;0x55...&lt;/code&gt; and libc around &lt;code&gt;0x7f...&lt;/code&gt;, which is about 45 TB away, if I subtracted qwords correctly in my head. The loader simply can't encode &lt;code&gt;call puts&lt;/code&gt; there, even if you ask. Every external call would have to become something like &lt;code&gt;movabs rax, &amp;lt;puts&amp;gt;; call rax&lt;/code&gt;, which is 12 bytes instead of 5, so the compiler would have to emit this long form everywhere in advance, just in case. That's what &lt;code&gt;-mcmodel=large&lt;/code&gt; actually does, and you'll see it in action below.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;So, text relocations. They turn up when a shared object is built from non-position-independent (i.e. good old, normal, non-PIC) code: in this case, the compiler puts an absolute address right into an instruction, and the only way to fix it at load time is to write into &lt;code&gt;.text&lt;/code&gt;. Let's make one and see how unhappy the toolchain can be about it. Here &lt;code&gt;-mcmodel=large&lt;/code&gt; makes the compiler load the address of &lt;code&gt;g&lt;/code&gt; with a 64-bit &lt;code&gt;movabs&lt;/code&gt;. Without it, the default small model would, as usual, use a 32-bit absolute address, which can't hold the address of a shared object loaded somewhere high by ASLR, so the linker refuses to build it at all instead of creating a text relocation:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; cat tr.c&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;/* non-PIC code that bakes an absolute address into .text */&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;long g = 42;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;long *get_addr(void) { return &amp;amp;g; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; gcc -shared -fno-pic -mcmodel&lt;span class="o"&gt;=&lt;/span&gt;large -o libtr.so tr.c&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;/usr/bin/ld: /tmp/cc058XuK.o: warning: relocation against `g&amp;#39; in read-only section `.text&amp;#39;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;/usr/bin/ld: warning: creating DT_TEXTREL in a shared object&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -d libtr.so &lt;span class="p"&gt;|&lt;/span&gt; grep TEXTREL&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x0000000000000016 (TEXTREL) 0x0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x000000000000001e (FLAGS) TEXTREL&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; objdump -d -M intel libtr.so&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;00000000000010f9 &amp;lt;get_addr&amp;gt;:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 10f9:&#9;55 &#9;push rbp&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 10fa:&#9;48 89 e5 &#9;mov rbp,rsp&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 10fd:&#9;48 b8 00 00 00 00 00 &#9;movabs rax,0x0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1104:&#9;00 00 00&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1107:&#9;5d &#9;pop rbp&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1108:&#9;c3 &#9;ret&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -r libtr.so &lt;span class="p"&gt;|&lt;/span&gt; grep &lt;span class="s2"&gt;&amp;#34;g\ &amp;#34;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0000000010ff 000500000001 R_X86_64_64 0000000000004008 g &amp;#43; 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;The linker still allows this, but it warns loudly and marks the result: it sets both the legacy &lt;code&gt;DT_TEXTREL&lt;/code&gt; tag and the &lt;code&gt;DF_TEXTREL&lt;/code&gt; bit in &lt;code&gt;DT_FLAGS&lt;/code&gt; (the two lines &lt;code&gt;readelf -d&lt;/code&gt; prints above). The 32-bit version, as I said, it refuses fiercely with &lt;code&gt;relocation R_X86_64_32 against symbol `g' can not be used when making a shared object; recompile with -fPIC&lt;/code&gt;. At runtime, it's the dynamic loader that has to deal with this, not the kernel, which maps the segment as &lt;code&gt;r-x&lt;/code&gt; as before. glibc's &lt;code&gt;_dl_relocate_object&lt;/code&gt; &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-reloc.c#L257" target="_blank" rel="noopener"&gt;checks &lt;code&gt;DT_TEXTREL&lt;/code&gt;&lt;/a&gt;, and for every &lt;code&gt;PT_LOAD&lt;/code&gt; segment without &lt;code&gt;PF_W&lt;/code&gt; it calls &lt;code&gt;__mprotect&lt;/code&gt; with &lt;code&gt;prot | PROT_WRITE&lt;/code&gt; (&lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-reloc.c#L281" target="_blank" rel="noopener"&gt;L281&lt;/a&gt;). The source comment there is quite honest about it: &lt;code&gt;/* Bletch. We must make read-only segments writable long enough to relocate them. */&lt;/code&gt;. Then it applies the relocations to the now-writable code and &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-reloc.c#L326" target="_blank" rel="noopener"&gt;restores the original protections&lt;/a&gt;. So for a while the page is writable and executable at the same time, which is the exact W^X problem from item 2. After patching, it's a private dirty copy, which is the sharing problem from item 1. Two big problems at once, just because some smart guy shipped non-PIC code. That's why text relocations are a bug, not a feature: the fix is always &lt;code&gt;-fPIC&lt;/code&gt; (and &lt;code&gt;-z text&lt;/code&gt; turns &lt;code&gt;DT_TEXTREL&lt;/code&gt; into a link error). &lt;code&gt;-fPIC&lt;/code&gt; is what makes the code go through the GOT instead, which is what the rest of this post is about.&lt;/p&gt;&#10;&lt;p&gt;So once you see all these constraints, the design is kind of obvious: please keep &lt;code&gt;.text&lt;/code&gt; read-only, position-independent and clean, and put all the &amp;quot;addresses I won't know until runtime&amp;quot; into one separate writable table of pointers. Call this table the Global Offset Table, or &lt;code&gt;.got&lt;/code&gt;, and keep going. The code will refer to external things indirectly, through a &amp;quot;GOT slot&amp;quot;: if you build with &lt;code&gt;-fno-plt&lt;/code&gt;, it's literally &lt;code&gt;call QWORD PTR [rip+puts@GOTPCREL]&lt;/code&gt;. The loader writes the real address into that slot, and voila, &lt;code&gt;.text&lt;/code&gt; never changes.&lt;/p&gt;&#10;&lt;h3 id="pie-or-when-even-your-own-addresses-move"&gt;PIE, or when even your own addresses move&lt;/h3&gt;&#10;&lt;p&gt;And even without shared libraries, there's PIE (position-independent executable), which has been the default &lt;code&gt;gcc&lt;/code&gt; output for years. With PIE, the problem above applies to the program's own code and data too. It's easy to check: a plain compile gives you an &lt;code&gt;ET_DYN&lt;/code&gt;, and &lt;code&gt;gcc&lt;/code&gt; says it was configured this way:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; gcc -o t t.c &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; readelf -h t &lt;span class="p"&gt;|&lt;/span&gt; grep Type &lt;span class="c1"&gt;# -no-pie would give EXEC&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Type: DYN (Position-Independent Executable file)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; gcc -v 2&amp;gt;&lt;span class="p"&gt;&amp;amp;&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; tr &lt;span class="s1"&gt;&amp;#39; &amp;#39;&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;\n&amp;#39;&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; grep enable-default-pie&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;--enable-default-pie&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;A PIE is an &lt;code&gt;ET_DYN&lt;/code&gt; object, so the kernel is free to map it at any address, and it does exactly that (hey, ASLR). You can see the split right in &lt;a href="https://github.com/torvalds/linux/blob/v6.12/fs/binfmt_elf.c#L1052" target="_blank" rel="noopener"&gt;&lt;code&gt;load_elf_binary()&lt;/code&gt;&lt;/a&gt;. An &lt;code&gt;ET_EXEC&lt;/code&gt; is mapped with &lt;a href="https://github.com/torvalds/linux/blob/v6.12/fs/binfmt_elf.c#L1058" target="_blank" rel="noopener"&gt;&lt;code&gt;MAP_FIXED_NOREPLACE&lt;/code&gt;&lt;/a&gt; at the &lt;code&gt;p_vaddr&lt;/code&gt; the linker put into the file, without any bias, so its address is fixed at link time. An ordinary PIE, i.e. an &lt;code&gt;ET_DYN&lt;/code&gt; with a &lt;code&gt;PT_INTERP&lt;/code&gt;, gets &lt;a href="https://github.com/torvalds/linux/blob/v6.12/fs/binfmt_elf.c#L1117-L1119" target="_blank" rel="noopener"&gt;&lt;code&gt;load_bias = ELF_ET_DYN_BASE&lt;/code&gt;&lt;/a&gt; plus a new random offset on every exec, &lt;a href="https://github.com/torvalds/linux/blob/v6.12/fs/binfmt_elf.c#L1120-L1121" target="_blank" rel="noopener"&gt;&lt;code&gt;load_bias += arch_mmap_rnd()&lt;/code&gt;&lt;/a&gt;, if the process has the &lt;code&gt;PF_RANDOMIZE&lt;/code&gt; flag. A static-PIE (an &lt;code&gt;ET_DYN&lt;/code&gt; without an interpreter) gets &lt;a href="https://github.com/torvalds/linux/blob/v6.12/fs/binfmt_elf.c#L1126-L1130" target="_blank" rel="noopener"&gt;&lt;code&gt;load_bias = 0&lt;/code&gt;&lt;/a&gt; instead and simply lands at the mmap base, which is already randomized. By the way, &lt;a href="https://github.com/torvalds/linux/blob/v6.12/arch/x86/include/asm/elf.h#L238-L239" target="_blank" rel="noopener"&gt;&lt;code&gt;ELF_ET_DYN_BASE&lt;/code&gt;&lt;/a&gt; on x86-64 is &lt;code&gt;DEFAULT_MAP_WINDOW / 3 * 2&lt;/code&gt;, two-thirds of the way up the address space, and that's where the &lt;code&gt;0x55...&lt;/code&gt; from item 4 comes from. So the program can't even hardcode the absolute address of its own global variables or functions into instructions. Two mechanisms work together here:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;For references inside the module (a function calling another function in the same binary, or taking the address of a global variable defined in it), the compiler uses RIP-relative addressing, like &lt;code&gt;lea rax,[rip+0xec0]&lt;/code&gt;. The distance between two things in the same module doesn't depend on the load address, so the linker can calculate the offset once, and no runtime fixup is needed.&lt;/li&gt;&#10;&lt;li&gt;For anything whose absolute address has to be stored somewhere in memory (a pointer in &lt;code&gt;.data&lt;/code&gt; that points into the module, &lt;code&gt;.init_array&lt;/code&gt; and &lt;code&gt;.fini_array&lt;/code&gt; entries, GOT slots holding the module's own addresses), the module has &lt;code&gt;R_X86_64_RELATIVE&lt;/code&gt; relocations, which simply do &amp;quot;add the load base to the value at this offset&amp;quot;. The loader walks through them at startup and rebases each one, and in glibc this is literally &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/sysdeps/x86_64/dl-machine.h#L499" target="_blank" rel="noopener"&gt;&lt;code&gt;*reloc_addr = l_addr + reloc-&amp;gt;r_addend&lt;/code&gt;&lt;/a&gt; in &lt;code&gt;elf_machine_rela_relative&lt;/code&gt;. &lt;code&gt;_dl_relocate_object&lt;/code&gt; calls it from a &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/do-rel.h#L114" target="_blank" rel="noopener"&gt;fast loop&lt;/a&gt; in &lt;code&gt;elf_dynamic_do_Rela&lt;/code&gt; that goes through all the &lt;code&gt;RELATIVE&lt;/code&gt; entries at the start of the table. Their number is what &lt;code&gt;readelf -d&lt;/code&gt; prints as the &lt;code&gt;RELACOUNT&lt;/code&gt; tag. These relocations are cheap, because they don't need any symbol lookup, and for the program's own addresses they're the only ones needed:&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; cat demo.c&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;#include &amp;lt;stdio.h&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;int main(void) {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; puts(&amp;#34;first call&amp;#34;);&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; puts(&amp;#34;second call&amp;#34;);&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; return 0;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;}&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; gcc -O0 -fno-stack-protector -fcf-protection&lt;span class="o"&gt;=&lt;/span&gt;none -o demo_partial demo.c -Wl,-z,relro,-z,lazy &lt;span class="c1"&gt;# partial RELRO, -fcf-protection=none keeps the classic PLT layout (more on that below)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -d demo_partial &lt;span class="p"&gt;|&lt;/span&gt; grep RELACOUNT &lt;span class="c1"&gt;# .init_array, .fini_array and __dso_handle&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x000000006ffffff9 (RELACOUNT) 3&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; gcc -O0 -fno-stack-protector -fcf-protection&lt;span class="o"&gt;=&lt;/span&gt;none -static-pie -o demo_staticpie demo.c &lt;span class="c1"&gt;# same source, built static-PIE&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -d demo_staticpie &lt;span class="p"&gt;|&lt;/span&gt; grep RELACOUNT &lt;span class="c1"&gt;# static-PIE has to rebase all of itself&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x000000006ffffff9 (RELACOUNT) 1099&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;Here's &lt;code&gt;main&lt;/code&gt; from this demo. One flag needs a word up front: I build every binary in this post with &lt;code&gt;-fcf-protection=none&lt;/code&gt;. Some toolchains enable &lt;a href="https://en.wikipedia.org/wiki/Control-flow_integrity#Intel_Control-flow_Enforcement_Technology" target="_blank" rel="noopener"&gt;Intel CET&lt;/a&gt; by default (Ubuntu's gcc, for example, but not Debian's), and CET splits the PLT into two sections, which makes every disassembly below more confusing and harder to follow. So turning it off gives the classic layout with a single &lt;code&gt;.plt&lt;/code&gt;, no matter where you build it. How and why CET splits the &lt;code&gt;.plt&lt;/code&gt; is explained in &lt;a href="#pltsec-what-cet-does-to-the-plt"&gt;its own section&lt;/a&gt; further down.&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; objdump -d -M intel demo_partial&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0000000000001139 &amp;lt;main&amp;gt;:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1139:&#9;55 &#9;push rbp&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 113a:&#9;48 89 e5 &#9;mov rbp,rsp&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 113d:&#9;48 8d 05 c0 0e 00 00 &#9;lea rax,[rip&amp;#43;0xec0] # 2004 &amp;lt;_IO_stdin_used&amp;#43;0x4&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1144:&#9;48 89 c7 &#9;mov rdi,rax&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1147:&#9;e8 e4 fe ff ff &#9;call 1030 &amp;lt;puts@plt&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 114c:&#9;48 8d 05 bc 0e 00 00 &#9;lea rax,[rip&amp;#43;0xebc] # 200f &amp;lt;_IO_stdin_used&amp;#43;0xf&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1153:&#9;48 89 c7 &#9;mov rdi,rax&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1156:&#9;e8 d5 fe ff ff &#9;call 1030 &amp;lt;puts@plt&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 115b:&#9;b8 00 00 00 00 &#9;mov eax,0x0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1160:&#9;5d &#9;pop rbp&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1161:&#9;c3 &#9;ret&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;I use this one &lt;code&gt;demo.c&lt;/code&gt; for every &lt;code&gt;demo_*&lt;/code&gt; binary in this post, only the compile and link flags are different. I'll show the &lt;code&gt;gcc&lt;/code&gt; command next to each binary when it first comes up, instead of dumping all of them here. And &lt;code&gt;-Wl,-z,lazy&lt;/code&gt; above forces lazy binding: some toolchains (Ubuntu's, for example, but again not Debian's) use &lt;code&gt;-z now&lt;/code&gt; by default, which resolves everything at startup and skips the path I want to show. More on that in &lt;a href="#full-relro-resolve-everything-up-front-then-freeze-it"&gt;the full RELRO section&lt;/a&gt;.&lt;/p&gt;&#10;&lt;p&gt;The strings are loaded RIP-relative without any fixups, but &lt;code&gt;puts&lt;/code&gt;, which is an external symbol, gets &lt;code&gt;call 1030 &amp;lt;puts@plt&amp;gt;&lt;/code&gt;. Note the &lt;code&gt;@plt&lt;/code&gt; suffix, which stands for Procedure Linkage Table in plain English. &lt;code&gt;puts@plt&lt;/code&gt; is a little stub, and this is where the fun begins.&lt;/p&gt;&#10;&lt;h2 id="the-cast-of-sections"&gt;The cast of sections&lt;/h2&gt;&#10;&lt;p&gt;Before going further, here are the sections involved, with a one-sentence description of each:&lt;/p&gt;&#10;&lt;table&gt;&#10;&#9;&lt;thead&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;Section&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;Perms&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;What it holds&lt;/th&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&lt;/thead&gt;&#10;&#9;&lt;tbody&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;.plt&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;r-xp&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Code stubs. Entry 0 (&lt;code&gt;PLT0&lt;/code&gt;) jumps into the resolver, and the rest are lazy stubs, one per function.&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;.got.plt&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;rw-p&lt;/code&gt;*&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;The writable pointer table for functions. &lt;code&gt;GOT[0..2]&lt;/code&gt; are reserved, and &lt;code&gt;GOT[3..]&lt;/code&gt; are the per-function slots that lazy binding patches.&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;.got&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;r--p&lt;/code&gt; after RELRO&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;The pointer table for everything else: data symbols and functions whose address is taken (resolved at startup via &lt;code&gt;GLOB_DAT&lt;/code&gt; to one canonical pointer, without a lazy stub).&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;.plt.sec&lt;/code&gt; / &lt;code&gt;.plt.got&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;r-xp&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Additional code stubs, for CET and for functions called through &lt;code&gt;.got&lt;/code&gt;. More on them below.&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&lt;/tbody&gt;&#10;&lt;/table&gt;&#10;&lt;p&gt;* Whether &lt;code&gt;.got.plt&lt;/code&gt; stays writable is what the RELRO (&amp;quot;relocation read-only&amp;quot;) magic is all about, so keep that in mind. Even with partial RELRO, its first three entries (&lt;code&gt;GOT[0..2]&lt;/code&gt;) end up on the read-only page, as you'll see in my pwndbg session below, and with full RELRO it doesn't even exist as a separate section.&lt;/p&gt;&#10;&lt;h2 id="partial-relro-lazy-binding-described-lazily-and-step-by-step"&gt;Partial RELRO: lazy binding, described lazily and step by step&lt;/h2&gt;&#10;&lt;p&gt;The idea of lazy binding is simple: don't resolve &lt;code&gt;puts&lt;/code&gt; until it's actually called for the first time (that's why it's lazy). So if a program imports 200 functions but calls only 5 of them, it pays for 5 lookups, not 200. The price for this is one more level of indirection and a clever resolver that patches the GOT slot on the first call.&lt;/p&gt;&#10;&lt;h3 id="the-classic-three-instruction-stub"&gt;The classic three-instruction stub&lt;/h3&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; objdump -d -M intel -j .plt demo_partial&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0000000000001020 &amp;lt;puts@plt-0x10&amp;gt;:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1020:&#9;ff 35 ca 2f 00 00 &#9;push QWORD PTR [rip&amp;#43;0x2fca] # 3ff0 &amp;lt;_GLOBAL_OFFSET_TABLE_&amp;#43;0x8&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1026:&#9;ff 25 cc 2f 00 00 &#9;jmp QWORD PTR [rip&amp;#43;0x2fcc] # 3ff8 &amp;lt;_GLOBAL_OFFSET_TABLE_&amp;#43;0x10&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 102c:&#9;0f 1f 40 00 &#9;nop DWORD PTR [rax&amp;#43;0x0]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0000000000001030 &amp;lt;puts@plt&amp;gt;:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1030:&#9;ff 25 ca 2f 00 00 &#9;jmp QWORD PTR [rip&amp;#43;0x2fca] # 4000 &amp;lt;puts@GLIBC_2.2.5&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1036:&#9;68 00 00 00 00 &#9;push 0x0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 103b:&#9;e9 e0 ff ff ff &#9;jmp 1020 &amp;lt;_init&amp;#43;0x20&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;And here's the GOT it points into, right from the file:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; objdump -s -j .got.plt demo_partial&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Contents of section .got.plt:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 3fe8 e03d0000 00000000 00000000 00000000 .=..............&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 3ff8 00000000 00000000 36100000 00000000 ........6.......&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;&lt;code&gt;GOT[0]&lt;/code&gt; in this dump is &lt;code&gt;0x3de0&lt;/code&gt; (&lt;code&gt;e03d0000&lt;/code&gt; in little-endian), which is the virtual address of &lt;code&gt;.dynamic&lt;/code&gt;, and also where &lt;code&gt;nm&lt;/code&gt; puts the &lt;code&gt;_DYNAMIC&lt;/code&gt; symbol:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -S ./demo_partial &lt;span class="p"&gt;|&lt;/span&gt; grep &lt;span class="s2"&gt;&amp;#34;\.dynamic&amp;#34;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; [22] .dynamic DYNAMIC 0000000000003de0 00002de0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; nm ./demo_partial &lt;span class="p"&gt;|&lt;/span&gt; grep _DYNAMIC&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0000000000003de0 d _DYNAMIC&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;So &lt;code&gt;GOT[0]&lt;/code&gt; holds the link-time address of &lt;code&gt;_DYNAMIC&lt;/code&gt;, i.e. this object's own &lt;code&gt;.dynamic&lt;/code&gt; array, before any relocation is applied. This is required by the x86-64 psABI (the processor-specific part of the System V ABI I mentioned at the beginning). The reason is a chicken-and-egg problem: &lt;code&gt;.dynamic&lt;/code&gt; is the table that lists the object's relocations (&lt;code&gt;DT_RELA&lt;/code&gt;, &lt;code&gt;DT_JMPREL&lt;/code&gt; and friends live there), so you have to read &lt;code&gt;.dynamic&lt;/code&gt; before you can apply any relocation, which means finding &lt;code&gt;.dynamic&lt;/code&gt; can't depend on a relocation itself. I like this &amp;quot;you can't relocate while you relocate&amp;quot; logic!&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;GOT[0]&lt;/code&gt; is supposed to break this circle: the linker puts the link-time address of &lt;code&gt;.dynamic&lt;/code&gt; into that slot as a plain value, which can be read without any relocation. The one who really needs it is the dynamic linker relocating itself (omg): the kernel maps &lt;code&gt;ld.so&lt;/code&gt; and jumps to its entry point with nothing resolved, and there's nobody else to tell this &amp;quot;relocating relocator&amp;quot; where its own &lt;code&gt;.dynamic&lt;/code&gt; is. Unlike &lt;code&gt;GOT[1]&lt;/code&gt; and &lt;code&gt;GOT[2]&lt;/code&gt;, nothing rewrites &lt;code&gt;GOT[0]&lt;/code&gt; at load time, and in my pwndbg session below it's still the raw &lt;code&gt;0x3de0&lt;/code&gt;, not &lt;code&gt;base + 0x3de0&lt;/code&gt;.&lt;/p&gt;&#10;&lt;p&gt;The idea is nice and simple, but in today's glibc this slot is more of a legacy: the x86-64 loader finds &lt;code&gt;_DYNAMIC&lt;/code&gt; with a RIP-relative reference and &lt;code&gt;__ehdr_start&lt;/code&gt; in &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/sysdeps/x86_64/dl-machine.h#L50-L64" target="_blank" rel="noopener"&gt;&lt;code&gt;elf_machine_dynamic&lt;/code&gt;&lt;/a&gt;, without reading &lt;code&gt;GOT[0]&lt;/code&gt; at all. The linker still fills the slot because the ABI says so, forever.&lt;/p&gt;&#10;&lt;p&gt;Unlike the first (or zeroth?) element, &lt;code&gt;GOT[1]&lt;/code&gt; and &lt;code&gt;GOT[2]&lt;/code&gt; are zeros in the file and get filled at load time. &lt;code&gt;GOT[3]&lt;/code&gt; is the actual slot for the address of &lt;code&gt;puts&lt;/code&gt;, and here's the first clever part: it's initialized to &lt;code&gt;0x1036&lt;/code&gt;, which is the address of the instruction right after the &lt;code&gt;jmp&lt;/code&gt; in &lt;code&gt;puts&lt;/code&gt;'s own stub.&lt;/p&gt;&#10;&lt;p&gt;Now let's trace the very first &lt;code&gt;call 1030 &amp;lt;puts@plt&amp;gt;&lt;/code&gt;:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;code&gt;jmp QWORD PTR [0x4000]&lt;/code&gt; jumps through the GOT slot of &lt;code&gt;puts&lt;/code&gt;. The slot contains &lt;code&gt;0x1036&lt;/code&gt;, so it jumps to &lt;code&gt;0x1036&lt;/code&gt;... which is the very next instruction. To an uninitiated reader it seems like an obfuscated no-op that lands one line below, but it's not like that. The slot is pre-loaded on purpose to bounce right back into the stub.&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;push 0x0&lt;/code&gt; pushes the relocation index for &lt;code&gt;puts&lt;/code&gt;. It's &lt;code&gt;0&lt;/code&gt; because &lt;code&gt;puts&lt;/code&gt; is the first (and only) entry in &lt;code&gt;.rela.plt&lt;/code&gt;:&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -r demo_partial&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;...&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Relocation section &amp;#39;.rela.plt&amp;#39; at offset 0x610 contains 1 entry:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Offset Info Type Sym. Value Sym. Name &amp;#43; Addend&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;000000004000 000300000007 R_X86_64_JUMP_SLO 0000000000000000 puts@GLIBC_2.2.5 &amp;#43; 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;The offset of this relocation is &lt;code&gt;0x4000&lt;/code&gt;, which is the GOT slot of &lt;code&gt;puts&lt;/code&gt;, and its index in the table is 0. This index is how the resolver knows what to look for.&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;code&gt;jmp 1020&lt;/code&gt; jumps to &lt;code&gt;PLT0&lt;/code&gt;.&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;PLT0&lt;/code&gt; does &lt;code&gt;push [GOT+0x08]&lt;/code&gt; and then &lt;code&gt;jmp [GOT+0x10]&lt;/code&gt;. The math is simple, as long as you remember that &lt;code&gt;rip&lt;/code&gt; points to the next instruction: the &lt;code&gt;push QWORD PTR [rip+0x2fca]&lt;/code&gt; at &lt;code&gt;0x1020&lt;/code&gt; is 6 bytes long, so it reads &lt;code&gt;0x1026 + 0x2fca = 0x3ff0&lt;/code&gt;, which is &lt;code&gt;GOT[1]&lt;/code&gt;. The same way, the &lt;code&gt;jmp QWORD PTR [rip+0x2fcc]&lt;/code&gt; at &lt;code&gt;0x1026&lt;/code&gt; reads &lt;code&gt;0x102c + 0x2fcc = 0x3ff8&lt;/code&gt;, which is &lt;code&gt;GOT[2]&lt;/code&gt;. So &lt;code&gt;PLT0&lt;/code&gt; pushes &lt;code&gt;GOT[1]&lt;/code&gt; and jumps through &lt;code&gt;GOT[2]&lt;/code&gt;. And the GOT base, &lt;code&gt;0x3fe8&lt;/code&gt;, is simply the start of &lt;code&gt;.got.plt&lt;/code&gt;, which is what &lt;code&gt;_GLOBAL_OFFSET_TABLE_&lt;/code&gt; points to:&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -S ./demo_partial &lt;span class="p"&gt;|&lt;/span&gt; grep &lt;span class="s2"&gt;&amp;#34;\.got\.plt&amp;#34;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; [24] .got.plt PROGBITS 0000000000003fe8 00002fe8&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;What's inside &lt;code&gt;GOT[1]&lt;/code&gt; and &lt;code&gt;GOT[2]&lt;/code&gt;? The loader sets them once, at load time, in &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/sysdeps/x86_64/dl-machine.h#L70" target="_blank" rel="noopener"&gt;&lt;code&gt;elf_machine_runtime_setup&lt;/code&gt;&lt;/a&gt;, and only if binding is lazy (&lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/sysdeps/x86_64/dl-machine.h#L78" target="_blank" rel="noopener"&gt;line 78&lt;/a&gt;):&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;code&gt;GOT[1]&lt;/code&gt; gets the &lt;code&gt;struct link_map *&lt;/code&gt; of this module, which is the loader's descriptor of the object whose symbol we're resolving (&lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/sysdeps/x86_64/dl-machine.h#L94" target="_blank" rel="noopener"&gt;line 94&lt;/a&gt;).&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;GOT[2]&lt;/code&gt; gets &lt;code&gt;GLRO(dl_x86_64_runtime_resolve)&lt;/code&gt;, the resolver trampoline (&lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/sysdeps/x86_64/dl-machine.h#L125-L126" target="_blank" rel="noopener"&gt;lines 125-126&lt;/a&gt;). This &lt;code&gt;GLRO&lt;/code&gt; field holds the variant the loader picked for this CPU at startup, &lt;code&gt;_dl_runtime_resolve_fxsave&lt;/code&gt;, &lt;code&gt;_xsave&lt;/code&gt; or &lt;code&gt;_xsavec&lt;/code&gt; (all three are built from the same &lt;code&gt;_dl_runtime_resolve&lt;/code&gt; source), so the slot doesn't get a fixed &lt;code&gt;&amp;amp;_dl_runtime_resolve&lt;/code&gt;.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;So &lt;code&gt;PLT0&lt;/code&gt; effectively calls &lt;code&gt;_dl_runtime_resolve(link_map *this_module, reloc_index)&lt;/code&gt;, where the relocation index was pushed by the stub of &lt;code&gt;puts&lt;/code&gt;, and the &lt;code&gt;link_map *&lt;/code&gt; by &lt;code&gt;PLT0&lt;/code&gt; itself. To add variety to a nerdy text, here is my pwndbg session (still no pictures) with the same &lt;code&gt;demo_partial&lt;/code&gt; binary as in the disassembly above. Try breaking at &lt;code&gt;main&lt;/code&gt; yourself and following it to the end, and your understanding will improve drastically, trust me. pwndbg's &lt;code&gt;$rebase(off)&lt;/code&gt; trick just adds the PIE base it detected, &lt;code&gt;0x555555554000&lt;/code&gt; here:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; b main&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Breakpoint 1 at 0x113d&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; r&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Breakpoint 1, 0x000055555555513d in main ()&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;# the image base and where the GOT lands: GOT[0..2] on a read-only page, the puts slot on the writable one&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; vmmap demo_partial&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;LEGEND: STACK | HEAP | CODE | DATA | WX | RODATA&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Start End Perm Size Offset File (set vmmap-prefer-relpaths on)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;► 0x555555554000 0x555555555000 r--p 1000 0 demo_partial&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;► 0x555555555000 0x555555556000 r-xp 1000 1000 demo_partial&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;► 0x555555556000 0x555555557000 r--p 1000 2000 demo_partial&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;► 0x555555557000 0x555555558000 r--p 1000 2000 demo_partial&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;► 0x555555558000 0x555555559000 rw-p 1000 3000 demo_partial&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x7ffff7dc3000 0x7ffff7dc6000 rw-p 3000 0 [anon_7ffff7dc3]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;# all three reserved slots: GOT[0] is the link-time &amp;amp;_DYNAMIC (0x3de0, the raw value from the dump above), GOT[1] is the link_map, GOT[2] is the resolver (telescope follows GOT[1] into the link_map)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; telescope $rebase(0x3fe8) 3&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;00:0000│ 0x555555557fe8 (_GLOBAL_OFFSET_TABLE_) ◂— 0x3de0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;01:0008│ 0x555555557ff0 (_GLOBAL_OFFSET_TABLE_&amp;#43;8) —▸ 0x7ffff7ffe5f0 —▸ 0x555555554000 ◂— 0x10102464c457f&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;02:0010│ 0x555555557ff8 (_GLOBAL_OFFSET_TABLE_&amp;#43;16) —▸ 0x7ffff7fda410 (_dl_runtime_resolve_xsavec) ◂— push rbx&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;# the puts slot before the first call, still pointing back into its own lazy stub&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; telescope $rebase(0x4000) 1&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;00:0000│ 0x555555558000 (puts@got[plt]) —▸ 0x555555555036 (puts@plt&amp;#43;6) ◂— push 0 /* &amp;#39;h&amp;#39; */&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;# break in the resolver on that first call and read its backtrace&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; b _dl_fixup&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Breakpoint 2 at 0x7ffff7fd8050: file ./elf/dl-runtime.c, line 47.&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; c&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Breakpoint 2, _dl_fixup (l=0x7ffff7ffe5f0, reloc_arg=0x0) at ./elf/dl-runtime.c:47&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;warning: 47&#9;./elf/dl-runtime.c: No such file or directory&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; bt 3&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;#0 _dl_fixup (l=0x7ffff7ffe5f0, reloc_arg=0x0) at ./elf/dl-runtime.c:47&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;#1 0x00007ffff7fda48a in _dl_runtime_resolve_xsavec () at ../sysdeps/x86_64/dl-trampoline.h:98&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;#2 0x000055555555514c in main ()&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;(More stack frames follow...)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;# let the resolver finish, then read the slot again: now it points straight to libc&amp;#39;s puts&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; d&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; finish&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Run till exit from #0 _dl_fixup (l=0x7ffff7ffe5f0, reloc_arg=0x0) at ./elf/dl-runtime.c:47&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;_dl_runtime_resolve_xsavec () at ../sysdeps/x86_64/dl-trampoline.h:99&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;warning: 99&#9;../sysdeps/x86_64/dl-trampoline.h: No such file or directory&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Value returned is $1 = 0x7ffff7e46300&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; got&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Filtering out read-only entries (display them with -r or --show-readonly)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;State of the GOT of /work/demo_partial:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;GOT protection: Partial RELRO | Found 1 GOT entries passing the filter&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;[0x555555558000] puts@GLIBC_2.2.5 -&amp;gt; 0x7ffff7e46300 (puts) ◂— push r14&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;Everything matches, wow. The first argument of the resolver, &lt;code&gt;l = 0x7ffff7ffe5f0&lt;/code&gt;, is the same value as in &lt;code&gt;GOT[1]&lt;/code&gt;, and &lt;code&gt;reloc_arg = 0&lt;/code&gt; is the index the &lt;code&gt;puts&lt;/code&gt; stub pushed. So &lt;code&gt;PLT0&lt;/code&gt; and the stub really gave &lt;code&gt;_dl_fixup&lt;/code&gt; exactly the two things it needs, and nothing more. Before the call, the &lt;code&gt;puts&lt;/code&gt; slot points to &lt;code&gt;puts@plt+6&lt;/code&gt; (its own &lt;code&gt;push 0; jmp PLT0&lt;/code&gt; lazy stub), and after it, the slot automagically points straight to libc's &lt;code&gt;puts&lt;/code&gt; at &lt;code&gt;0x7ffff7e46300&lt;/code&gt;. So the second &lt;code&gt;puts(&amp;quot;second call&amp;quot;)&lt;/code&gt; skips the whole detour and jumps there directly.&lt;/p&gt;&#10;&lt;p&gt;Now let's map these addresses onto the &lt;code&gt;vmmap&lt;/code&gt; above. &lt;code&gt;GOT[0]&lt;/code&gt;, &lt;code&gt;GOT[1]&lt;/code&gt; and &lt;code&gt;GOT[2]&lt;/code&gt; are at &lt;code&gt;...557fe8&lt;/code&gt;, &lt;code&gt;...557ff0&lt;/code&gt; and &lt;code&gt;...557ff8&lt;/code&gt;, at the very end of the &lt;code&gt;r--p&lt;/code&gt; page &lt;code&gt;...557000-...558000&lt;/code&gt;. The &lt;code&gt;puts&lt;/code&gt; slot is the next eight bytes, at &lt;code&gt;...558000&lt;/code&gt;, which is where the &lt;code&gt;rw-p&lt;/code&gt; page &lt;code&gt;...558000-...559000&lt;/code&gt; begins. So &lt;code&gt;.got.plt&lt;/code&gt; crosses the page boundary: its three reserved entries are on the read-only page, and the &lt;code&gt;JUMP_SLOT&lt;/code&gt; array (starting with &lt;code&gt;puts&lt;/code&gt;) is on the writable one. This split is on purpose, and we'll come back to it twice: right after the resolver, and when full RELRO suddenly changes everything.&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;GOT[1]&lt;/code&gt; points into the data of &lt;code&gt;ld.so&lt;/code&gt; itself, because it's the &lt;code&gt;link_map&lt;/code&gt; of this object, not code. &lt;code&gt;telescope&lt;/code&gt; even follows it one step further, to the &lt;code&gt;l_addr&lt;/code&gt; field of the struct (&lt;code&gt;0x555555554000&lt;/code&gt;, the load base), and to the ELF header sitting there: &lt;code&gt;0x10102464c457f&lt;/code&gt; is just the first eight bytes &lt;code&gt;7f 45 4c 46 02 01 01 00&lt;/code&gt; read as a little-endian number, i.e. &lt;code&gt;\x7fELF&lt;/code&gt;, 64-bit, little-endian, version 1. &lt;code&gt;GOT[2]&lt;/code&gt; is &lt;code&gt;_dl_runtime_resolve_xsavec&lt;/code&gt;, not &lt;code&gt;_xsave&lt;/code&gt; or &lt;code&gt;_fxsave&lt;/code&gt;, because this CPU supports &lt;code&gt;xsavec&lt;/code&gt; (check with &lt;code&gt;grep xsavec /proc/cpuinfo&lt;/code&gt;). This choice matters for the next part.&lt;/p&gt;&#10;&lt;h3 id="what-_dl_runtime_resolve-has-to-be-careful-about"&gt;What &lt;code&gt;_dl_runtime_resolve&lt;/code&gt; has to be careful about&lt;/h3&gt;&#10;&lt;p&gt;&lt;code&gt;_dl_runtime_resolve&lt;/code&gt; runs in the middle of your function call: you've already set up the arguments for &lt;code&gt;puts&lt;/code&gt;, but &lt;code&gt;puts&lt;/code&gt; hasn't run yet, and &lt;code&gt;rdi&lt;/code&gt; already holds the string pointer. And the resolver is about to run a big pile of C code (symbol lookup goes through hash tables, string comparisons and so on) that will clobber every caller-saved register. If it then jumps to &lt;code&gt;puts&lt;/code&gt; with a trashed &lt;code&gt;rdi&lt;/code&gt;, &lt;code&gt;puts&lt;/code&gt; prints garbage or segfaults.&lt;/p&gt;&#10;&lt;p&gt;So the first job of the trampoline is to save all the registers used for passing arguments, and to restore them after the resolve. The &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/sysdeps/x86_64/dl-trampoline.h#L64-L71" target="_blank" rel="noopener"&gt;asm code&lt;/a&gt; saves &lt;code&gt;rax&lt;/code&gt;, &lt;code&gt;rcx&lt;/code&gt;, &lt;code&gt;rdx&lt;/code&gt;, &lt;code&gt;rsi&lt;/code&gt;, &lt;code&gt;rdi&lt;/code&gt;, &lt;code&gt;r8&lt;/code&gt; and &lt;code&gt;r9&lt;/code&gt; by hand (glibc's source uses the AT&amp;amp;T syntax, but I converted it to Intel to match the rest of the post):&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-nasm" data-lang="nasm"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#9;&lt;span class="nf"&gt;mov&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;rsp&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nv"&gt;REGISTER_SAVE_RAX&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nb"&gt;rax&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#9;&lt;span class="nf"&gt;mov&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;rsp&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nv"&gt;REGISTER_SAVE_RCX&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nb"&gt;rcx&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#9;&lt;span class="nf"&gt;mov&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;rsp&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nv"&gt;REGISTER_SAVE_RDX&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nb"&gt;rdx&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#9;&lt;span class="nf"&gt;mov&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;rsp&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nv"&gt;REGISTER_SAVE_RSI&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nb"&gt;rsi&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#9;&lt;span class="nf"&gt;mov&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;rsp&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nv"&gt;REGISTER_SAVE_RDI&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nb"&gt;rdi&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#9;&lt;span class="nf"&gt;mov&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;rsp&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nv"&gt;REGISTER_SAVE_R8&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nb"&gt;r8&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#9;&lt;span class="nf"&gt;mov&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;rsp&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nv"&gt;REGISTER_SAVE_R9&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nb"&gt;r9&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#9;&lt;span class="nf"&gt;...&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#9;&lt;span class="nf"&gt;fxsave&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;rsp&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nv"&gt;STATE_SAVE_OFFSET&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;; or xsave/xsavec, depending on the CPU&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;The integer registers are the six SysV argument registers plus &lt;code&gt;rax&lt;/code&gt;, which holds the number of vector registers used by a variadic call. Then &lt;code&gt;fxsave&lt;/code&gt;, &lt;code&gt;xsave&lt;/code&gt; or &lt;code&gt;xsavec&lt;/code&gt; saves the vector state, because the arguments can also be floats or vectors in &lt;code&gt;XMM&lt;/code&gt;, &lt;code&gt;YMM&lt;/code&gt; or &lt;code&gt;ZMM&lt;/code&gt; registers, and the resolver's own code (&lt;code&gt;memcpy&lt;/code&gt; and friends, string functions) will use them too. That's why there are several variants of the resolver: &lt;code&gt;_dl_runtime_resolve_fxsave&lt;/code&gt; for old CPUs without XSAVE (it only knows the SSE registers), and &lt;code&gt;_xsave&lt;/code&gt; or &lt;code&gt;_xsavec&lt;/code&gt; for everything newer. These two save the same state, &lt;code&gt;xsavec&lt;/code&gt; just does it in a compacted form, which is faster, so the loader picks it whenever the CPU supports it. In other words, the whole thing is one big &amp;quot;I'm about to break everything, so let me put it all back exactly as it was&amp;quot; wrapper around the resolve.&lt;/p&gt;&#10;&lt;h3 id="the-resolving-logic-itself"&gt;The resolving logic itself&lt;/h3&gt;&#10;&lt;p&gt;With the registers saved, the trampoline calls &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-runtime.c#L41" target="_blank" rel="noopener"&gt;&lt;code&gt;_dl_fixup(struct link_map *l, reloc_arg)&lt;/code&gt;&lt;/a&gt;, where &lt;code&gt;l&lt;/code&gt; is the &lt;code&gt;link_map&lt;/code&gt; that &lt;code&gt;PLT0&lt;/code&gt; pushed from &lt;code&gt;GOT[1]&lt;/code&gt;, and &lt;code&gt;reloc_arg&lt;/code&gt; is the index the &lt;code&gt;puts&lt;/code&gt; stub pushed (&lt;code&gt;0&lt;/code&gt;). Its whole job is to turn this number into the real address of &lt;code&gt;puts&lt;/code&gt; and save it in the proper GOT slot, and every step is (mostly) a table lookup starting from &lt;code&gt;l&lt;/code&gt;. I'd rather describe the logic than paste the raw code, because it will be a bit more understandable that way.&lt;/p&gt;&#10;&lt;p&gt;First, &lt;code&gt;_dl_fixup&lt;/code&gt; needs to find the relocation to work with. &lt;code&gt;reloc_arg&lt;/code&gt; isn't a pointer to something useful, it's just an index. &lt;code&gt;_dl_fixup&lt;/code&gt; takes the address of &lt;code&gt;.rela.plt&lt;/code&gt; from the &lt;code&gt;DT_JMPREL&lt;/code&gt; entry in the &lt;code&gt;.dynamic&lt;/code&gt; section (glibc keeps a pointer to every such entry in &lt;code&gt;l-&amp;gt;l_info[]&lt;/code&gt;), and &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-runtime.c#L53-L55" target="_blank" rel="noopener"&gt;uses the index to pick one &lt;code&gt;Elf64_Rela&lt;/code&gt;&lt;/a&gt; from it. This single entry has everything it needs: its &lt;code&gt;r_offset&lt;/code&gt; says where the &lt;code&gt;puts&lt;/code&gt; slot is (&lt;code&gt;0x4000&lt;/code&gt; here, relative to the load address), and its &lt;code&gt;r_info&lt;/code&gt; packs together the relocation type (&lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-runtime.c#L63" target="_blank" rel="noopener"&gt;&lt;code&gt;R_X86_64_JUMP_SLOT&lt;/code&gt;, checked by an &lt;code&gt;assert&lt;/code&gt;&lt;/a&gt;) and the symbol index.&lt;/p&gt;&#10;&lt;p&gt;Then it needs to link this relocation to a symbol. The symbol index from &lt;code&gt;r_info&lt;/code&gt; &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-runtime.c#L56" target="_blank" rel="noopener"&gt;picks one &lt;code&gt;Elf64_Sym&lt;/code&gt;&lt;/a&gt; in the &lt;code&gt;.dynsym&lt;/code&gt; of this object. This symbol's &lt;code&gt;st_name&lt;/code&gt; is a byte offset into &lt;code&gt;.dynstr&lt;/code&gt;, and the string there is the name the caller wants: &lt;code&gt;&amp;quot;puts&amp;quot;&lt;/code&gt;. So the whole chain is index -&amp;gt; &lt;code&gt;Elf64_Rela&lt;/code&gt; -&amp;gt; symbol index -&amp;gt; &lt;code&gt;Elf64_Sym&lt;/code&gt; -&amp;gt; &lt;code&gt;&amp;quot;puts&amp;quot;&lt;/code&gt;, and every step is a simple read from a table the &lt;code&gt;link_map&lt;/code&gt; already points to.&lt;/p&gt;&#10;&lt;p&gt;Next, it needs to find where &lt;code&gt;puts&lt;/code&gt; actually lives. Someone's name still doesn't tell you where exactly to find them: &lt;code&gt;puts&lt;/code&gt; is defined in some other object, and which one depends on the search scope. The scope is an ordered list of objects that the loader builds at startup. &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-deps.c#L156-L157" target="_blank" rel="noopener"&gt;The executable goes first&lt;/a&gt; (&lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-deps.c#L159-L161" target="_blank" rel="noopener"&gt;right after it come the &lt;code&gt;LD_PRELOAD&lt;/code&gt; libraries&lt;/a&gt;, more on them below), then every library reachable through &lt;code&gt;DT_NEEDED&lt;/code&gt;, in breadth-first order (the direct dependencies of an object come before their own dependencies), and each object appears there only once. That's &lt;code&gt;l-&amp;gt;l_searchlist&lt;/code&gt;, and the loader builds it in &lt;code&gt;_dl_map_object_deps&lt;/code&gt;, where &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-deps.c#L177" target="_blank" rel="noopener"&gt;the comment&lt;/a&gt; calls it a &amp;quot;breadth-first search of the dependency tree.&amp;quot;&lt;/p&gt;&#10;&lt;p&gt;Then &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-runtime.c#L95" target="_blank" rel="noopener"&gt;&lt;code&gt;_dl_lookup_symbol_x&lt;/code&gt;&lt;/a&gt; passes the name to &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-lookup.c#L329" target="_blank" rel="noopener"&gt;&lt;code&gt;do_lookup_x&lt;/code&gt;&lt;/a&gt;, which walks this list from the start and looks for an entry named &lt;code&gt;&amp;quot;puts&amp;quot;&lt;/code&gt; in the symbol table of each object. This entry has to define &lt;code&gt;puts&lt;/code&gt;, not just reference it, which turns out to be an important difference. How exactly it searches inside one object is the &lt;code&gt;.gnu.hash&lt;/code&gt; lookup, which gets &lt;a href="#gnuhash-vs-hash-finding-puts-in-libc-fast"&gt;its own section&lt;/a&gt; further down.&lt;/p&gt;&#10;&lt;p&gt;A &lt;code&gt;.dynsym&lt;/code&gt; can have two kinds of entries for the same name. A definition has a real section index in &lt;code&gt;st_shndx&lt;/code&gt;, which means the symbol actually has an address in this object. An undefined reference (&lt;code&gt;st_shndx == SHN_UNDEF&lt;/code&gt;, value &lt;code&gt;0&lt;/code&gt;) only says that the object uses this name somewhere and needs someone else to provide it. &lt;code&gt;do_lookup_x&lt;/code&gt; wants the first kind and skips the second: its helper &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-lookup.c#L73-L77" target="_blank" rel="noopener"&gt;&lt;code&gt;check_match&lt;/code&gt;&lt;/a&gt; drops entries without a value, and for a PLT lookup like this one (there are others), any &lt;code&gt;SHN_UNDEF&lt;/code&gt; entry too. And this isn't just another &lt;code&gt;if&lt;/code&gt; in the code: the demo executable's own &lt;code&gt;.dynsym&lt;/code&gt; has a &lt;code&gt;puts&lt;/code&gt; entry, but only as an undefined reference (it calls &lt;code&gt;puts&lt;/code&gt; but doesn't define it), while the real definition lives in &lt;code&gt;libc.so.6&lt;/code&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf --dyn-syms demo_partial &lt;span class="p"&gt;|&lt;/span&gt; grep puts&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 3: 0000000000000000 0 FUNC GLOBAL DEFAULT UND puts@GLIBC_2.2.5 (3)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf --dyn-syms /lib/x86_64-linux-gnu/libc.so.6 &lt;span class="p"&gt;|&lt;/span&gt; grep &lt;span class="s1"&gt;&amp;#39; puts@@&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 236: 0000000000080300 514 FUNC WEAK DEFAULT 15 puts@@GLIBC_2.2.5&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;where &lt;code&gt;UND&lt;/code&gt; in the first line is the undefined reference (no address), and &lt;code&gt;15&lt;/code&gt; in the second one is the index of the section where &lt;code&gt;puts&lt;/code&gt; is defined, which is &lt;code&gt;.text&lt;/code&gt; in libc.&lt;/p&gt;&#10;&lt;p&gt;So the scan skips the executable (its &lt;code&gt;puts&lt;/code&gt; is just a reference) and goes on until it finds the real definition in &lt;code&gt;libc.so.6&lt;/code&gt;. The first object in the list that has a definition wins: the scan stops there and returns this &lt;code&gt;Elf64_Sym&lt;/code&gt; together with the &lt;code&gt;link_map&lt;/code&gt; of the object it belongs to.&lt;/p&gt;&#10;&lt;p&gt;By the way, the &lt;code&gt;puts&lt;/code&gt; in libc is &lt;code&gt;WEAK&lt;/code&gt; in the listing above (libc defines it as a &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/libio/ioputs.c#L48" target="_blank" rel="noopener"&gt;weak alias&lt;/a&gt; of &lt;code&gt;_IO_puts&lt;/code&gt;), and it still wins. The static linker cares about weak vs global, but the dynamic one doesn't: in &lt;code&gt;do_lookup_x&lt;/code&gt;, a weak definition &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-lookup.c#L472-L483" target="_blank" rel="noopener"&gt;falls through&lt;/a&gt; to the same code as a global one, so the first definition in the list wins anyway. The old &amp;quot;keep looking for a global one&amp;quot; behavior comes back only if you set &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/rtld.c#L2662-L2663" target="_blank" rel="noopener"&gt;&lt;code&gt;LD_DYNAMIC_WEAK&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;&#10;&lt;p&gt;This &amp;quot;first match wins&amp;quot; rule is what the idea of symbol interposition is based on. The executable is at the beginning of the list, so a function it defines hides the libc function with the same name. And the &lt;code&gt;LD_PRELOAD&lt;/code&gt; libraries come right after the executable, before the usual dependencies, so they can do the same.&lt;/p&gt;&#10;&lt;p&gt;Time for a break from the nerdy text, so let's do a small experiment and see what this order changes. Here's a tiny interposer that defines its own &lt;code&gt;puts&lt;/code&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// preload_puts.c&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;unistd.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;string.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cm"&gt;/* Our own puts(): LD_PRELOAD puts this library ahead of libc, so this one&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cm"&gt; wins. Only write(2) inside, because gcc turns printf(&amp;#34;%s\n&amp;#34;, s) into a&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cm"&gt; puts() call, and we&amp;#39;d end up in a cute school-level recursion bug. */&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;puts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;marker&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;&amp;#34;[preload puts] &amp;#34;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;marker&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt; &lt;span class="n"&gt;marker&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;strlen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;#34;&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Build it as a shared library, then run the program with it preloaded. &lt;code&gt;LD_DEBUG=scopes&lt;/code&gt; prints the search scope that the loader built, and you can see that every object uses the same global list, &lt;code&gt;scope 0&lt;/code&gt; (the &lt;code&gt;185:&lt;/code&gt; at the start of each line is just the PID that ld.so adds):&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; gcc -shared -fPIC -o preload_puts.so preload_puts.c&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; &lt;span class="nv"&gt;LD_DEBUG&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;scopes &lt;span class="nv"&gt;LD_PRELOAD&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;./preload_puts.so ./demo_partial&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 62:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 62:&#9;Initial object scopes&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 62:&#9;object=./demo_partial [0]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 62:&#9; scope 0: ./demo_partial ./preload_puts.so /lib/x86_64-linux-gnu/libc.so.6 /lib64/ld-linux-x86-64.so.2&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 62:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 62:&#9;object=linux-vdso.so.1 [0]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 62:&#9; scope 0: ./demo_partial ./preload_puts.so /lib/x86_64-linux-gnu/libc.so.6 /lib64/ld-linux-x86-64.so.2&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 62:&#9; scope 1: linux-vdso.so.1&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 62:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 62:&#9;object=./preload_puts.so [0]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 62:&#9; scope 0: ./demo_partial ./preload_puts.so /lib/x86_64-linux-gnu/libc.so.6 /lib64/ld-linux-x86-64.so.2&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 62:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 62:&#9;object=/lib/x86_64-linux-gnu/libc.so.6 [0]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 62:&#9; scope 0: ./demo_partial ./preload_puts.so /lib/x86_64-linux-gnu/libc.so.6 /lib64/ld-linux-x86-64.so.2&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 62:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 62:&#9;object=/lib64/ld-linux-x86-64.so.2 [0]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 62:&#9; no scope&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; &lt;span class="nv"&gt;LD_PRELOAD&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;./preload_puts.so ./demo_partial&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;[preload puts] first call&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;[preload puts] second call&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;Every object has the same global &lt;code&gt;scope 0&lt;/code&gt;, and in it &lt;code&gt;./preload_puts.so&lt;/code&gt; is right after &lt;code&gt;./demo_partial&lt;/code&gt; and before &lt;code&gt;libc.so.6&lt;/code&gt;. So a lookup of &lt;code&gt;puts&lt;/code&gt; finds the definition in the preloaded library first and stops there. That's how a preloaded &lt;code&gt;malloc&lt;/code&gt; (tcmalloc/jemalloc and friends) replaces the libc one for the whole process. &lt;code&gt;ld-linux&lt;/code&gt; itself shows &amp;quot;no scope&amp;quot; because its own &lt;code&gt;link_map&lt;/code&gt; &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-open.c#L936-L951" target="_blank" rel="noopener"&gt;never gets a scope list&lt;/a&gt; (remember, it relocates itself in its own special way), but it's still the last object in everyone's &lt;code&gt;scope 0&lt;/code&gt;. One more thing comes from the relocation itself: the reference is &lt;code&gt;puts@GLIBC_2.2.5&lt;/code&gt;, so a candidate has to match the symbol version too, not just the name. The loader won't bind to a &lt;code&gt;puts&lt;/code&gt; with a different version tag, but it &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-lookup.c#L94-L108" target="_blank" rel="noopener"&gt;accepts a definition without any version&lt;/a&gt;, like ours, otherwise this experiment wouldn't work.&lt;/p&gt;&#10;&lt;p&gt;One more weird thing in this output: &lt;code&gt;linux-vdso.so.1&lt;/code&gt; is the only object with a second scope (&lt;code&gt;scope 1: linux-vdso.so.1&lt;/code&gt;). This quirk comes from how &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-object.c#L155-L157" target="_blank" rel="noopener"&gt;&lt;code&gt;_dl_new_object&lt;/code&gt;&lt;/a&gt; gives out scopes. Every object loaded after the main program gets the global list as &lt;code&gt;scope 0&lt;/code&gt;, and it &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-object.c#L168" target="_blank" rel="noopener"&gt;gets a second, local scope&lt;/a&gt; only if that one is somehow different from the global list. For a &lt;code&gt;DT_NEEDED&lt;/code&gt; dependency or an &lt;code&gt;LD_PRELOAD&lt;/code&gt; library, the chain of loaders goes up to the executable, whose search list is the global list, so both scopes are the same and there's no second one.&lt;/p&gt;&#10;&lt;p&gt;But the vDSO is &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/setup-vdso.h#L32" target="_blank" rel="noopener"&gt;created without a loader&lt;/a&gt;, so it becomes its own loader (hi to the &amp;quot;relocating relocator&amp;quot; from above), and its local scope is &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/setup-vdso.h#L70-L75" target="_blank" rel="noopener"&gt;a list with just itself in it&lt;/a&gt;, which is different from the global list. Hence the extra &lt;code&gt;scope 1&lt;/code&gt;. The kernel maps the vDSO into the process by itself, so it's all alone: it isn't in anyone's &lt;code&gt;DT_NEEDED&lt;/code&gt;, and it isn't in the global list either. This cozy private scope is how glibc looks up symbols inside the vDSO (&lt;code&gt;__vdso_clock_gettime&lt;/code&gt; and friends), and that's what the comment there says it's for.&lt;/p&gt;&#10;&lt;p&gt;Now it can compute the address, and that's just arithmetic. The &lt;code&gt;st_value&lt;/code&gt; of the found symbol is the offset of &lt;code&gt;puts&lt;/code&gt; inside libc, and the &lt;code&gt;link_map&lt;/code&gt; of libc keeps its load address in &lt;code&gt;l_addr&lt;/code&gt;. &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/sysdeps/generic/ldsodefs.h#L94-L101" target="_blank" rel="noopener"&gt;&lt;code&gt;SYMBOL_ADDRESS&lt;/code&gt; adds them together&lt;/a&gt;, and the result is the real address of &lt;code&gt;puts&lt;/code&gt;: the same &lt;code&gt;0x7ffff7e46300&lt;/code&gt; that the &lt;code&gt;got&lt;/code&gt; command printed above (&lt;code&gt;0x7ffff7dc6000 + 0x80300&lt;/code&gt;, if you're so curious).&lt;/p&gt;&#10;&lt;p&gt;And finally, &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/sysdeps/x86_64/dl-machine.h#L218" target="_blank" rel="noopener"&gt;&lt;code&gt;elf_machine_fixup_plt&lt;/code&gt;&lt;/a&gt; writes this address into the slot from &lt;code&gt;r_offset&lt;/code&gt;. Its whole body is a modest &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/sysdeps/x86_64/dl-machine.h#L223" target="_blank" rel="noopener"&gt;&lt;code&gt;return *reloc_addr = value;&lt;/code&gt;&lt;/a&gt;, so it also returns the address, and &lt;code&gt;_dl_fixup&lt;/code&gt; passes it back to the trampoline. The trampoline restores the registers it saved (so &lt;code&gt;rdi&lt;/code&gt; points to the string again, as if nothing happened) and jumps to that address. And yay, &lt;code&gt;puts&lt;/code&gt; finally runs.&lt;/p&gt;&#10;&lt;p&gt;So the first call pays for the full symbol lookup and, as a side effect, rewrites its own GOT slot. That's the change we saw in pwndbg above, from the lazy stub to &lt;code&gt;0x7ffff7e46300&lt;/code&gt;. Every next &lt;code&gt;call puts@plt&lt;/code&gt; then just does &lt;code&gt;jmp [slot]&lt;/code&gt; straight into libc: one indirection, no resolver, forever. And that's what lazy binding is about: each imported function is resolved once, on its first call, and after that only the first instruction of its PLT stub ever runs. A simple idea that's quite hard to implement properly, as usual.&lt;/p&gt;&#10;&lt;h3 id="and-the-relro-part-of-partial-relro"&gt;And the RELRO part of partial RELRO&lt;/h3&gt;&#10;&lt;p&gt;Now back to the &lt;code&gt;vmmap&lt;/code&gt; above, where &lt;code&gt;GOT[0..2]&lt;/code&gt; ended up on a read-only page and the &lt;code&gt;puts&lt;/code&gt; slot on a writable one. The loader writes &lt;code&gt;GOT[1]&lt;/code&gt; and &lt;code&gt;GOT[2]&lt;/code&gt; once at startup and never touches them again, so they really can be read-only, while lazy binding keeps writing into the &lt;code&gt;puts&lt;/code&gt; slot, which comes right after them.&lt;/p&gt;&#10;&lt;p&gt;And it's not only about the GOT: partial RELRO (&lt;code&gt;-z relro&lt;/code&gt; without &lt;code&gt;-z now&lt;/code&gt;) makes read-only everything the loader is done with after startup, which is &lt;code&gt;.init_array&lt;/code&gt;, &lt;code&gt;.fini_array&lt;/code&gt;, &lt;code&gt;.dynamic&lt;/code&gt;, &lt;code&gt;.got&lt;/code&gt; and the first three entries of &lt;code&gt;.got.plt&lt;/code&gt;. All of them live in the same writable segment (the &lt;code&gt;RW&lt;/code&gt; &lt;code&gt;LOAD&lt;/code&gt; one at &lt;code&gt;0x3dd0&lt;/code&gt;). To tell the loader which exact part of it to lock, the linker adds an additional program header, &lt;code&gt;PT_GNU_RELRO&lt;/code&gt;, which is basically an address range. Here is how to see it with &lt;code&gt;readelf&lt;/code&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -lW demo_partial&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; ...&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; LOAD 0x002dd0 0x0000000000003dd0 0x0000000000003dd0 0x000248 0x000250 RW 0x1000&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; ...&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; GNU_RELRO 0x002dd0 0x0000000000003dd0 0x0000000000003dd0 0x000230 0x000230 R 0x1&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Section to Segment mapping:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Segment Sections...&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; ...&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 05 .init_array .fini_array .dynamic .got .got.plt .data .bss&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; ...&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 13 .init_array .fini_array .dynamic .got&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;So &lt;code&gt;GNU_RELRO&lt;/code&gt; (number &lt;code&gt;13&lt;/code&gt; in the list) covers the range from &lt;code&gt;0x3dd0&lt;/code&gt; to &lt;code&gt;0x3dd0 + 0x230 = 0x4000&lt;/code&gt;. That's the whole &lt;code&gt;.init_array&lt;/code&gt;, &lt;code&gt;.fini_array&lt;/code&gt;, &lt;code&gt;.dynamic&lt;/code&gt; and &lt;code&gt;.got&lt;/code&gt;, plus the first 24 bytes of &lt;code&gt;.got.plt&lt;/code&gt; (&lt;code&gt;0x3fe8&lt;/code&gt; to &lt;code&gt;0x4000&lt;/code&gt;, our beloved &lt;code&gt;GOT[0..2]&lt;/code&gt;). &lt;code&gt;readelf&lt;/code&gt; doesn't list &lt;code&gt;.got.plt&lt;/code&gt; in segment &lt;code&gt;13&lt;/code&gt; because it shows only the sections that fit into a segment completely. The rest (the &lt;code&gt;puts&lt;/code&gt; slot at &lt;code&gt;0x4000&lt;/code&gt;, &lt;code&gt;.data&lt;/code&gt; and &lt;code&gt;.bss&lt;/code&gt;) is outside the range, so it stays writable, like the whole &lt;code&gt;LOAD&lt;/code&gt; segment &lt;code&gt;05&lt;/code&gt;.&lt;/p&gt;&#10;&lt;p&gt;But how does this range suddenly become read-only, if the kernel maps the whole segment as &lt;code&gt;RW&lt;/code&gt;? The loader does it itself. The segment has to be writable at first, because the loader writes quite a lot into it at startup: the &lt;code&gt;R_X86_64_RELATIVE&lt;/code&gt; relocations go into &lt;code&gt;.init_array&lt;/code&gt; and &lt;code&gt;.fini_array&lt;/code&gt;, and the &lt;code&gt;GLOB_DAT&lt;/code&gt; ones into &lt;code&gt;.got&lt;/code&gt;. Only when all relocations are done, &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-reloc.c#L346-L347" target="_blank" rel="noopener"&gt;&lt;code&gt;_dl_relocate_object&lt;/code&gt; calls &lt;code&gt;_dl_protect_relro&lt;/code&gt;&lt;/a&gt;, which &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-reloc.c#L356-L364" target="_blank" rel="noopener"&gt;rounds the range down to page boundaries and &lt;code&gt;mprotect&lt;/code&gt;s it&lt;/a&gt; as &lt;code&gt;PROT_READ&lt;/code&gt;. Here it's one page, from base &lt;code&gt;+0x3000&lt;/code&gt; to base &lt;code&gt;+0x4000&lt;/code&gt;. So &lt;code&gt;RW&lt;/code&gt; in the program header is only the initial permission, and the loader lowers it to &lt;code&gt;r--&lt;/code&gt; as soon as nothing needs to write there anymore. It's the same trick as with &lt;code&gt;DT_TEXTREL&lt;/code&gt; earlier, but in the opposite direction: there the loader adds write permission for a moment to patch the code, and here it removes it for good after the relocations.&lt;/p&gt;&#10;&lt;p&gt;Both ends of the range are rounded down, and that's why the linker ends the RELRO range at &lt;code&gt;0x4000&lt;/code&gt; and puts the &lt;code&gt;puts&lt;/code&gt; (writes the &lt;code&gt;write&lt;/code&gt;?) slot right after it. If the range ended in the middle of a page, this whole page would stay writable, because &lt;code&gt;mprotect&lt;/code&gt; can't protect half a page. So the default linker script does it on purpose: it ends the RELRO region 24 bytes into &lt;code&gt;.got.plt&lt;/code&gt; (three 8-byte slots) and shifts the layout so that this point lands exactly on a page boundary. If you're brave enough, look for &lt;code&gt;DATA_SEGMENT_RELRO_END&lt;/code&gt; in &lt;code&gt;ld --verbose&lt;/code&gt;.&lt;/p&gt;&#10;&lt;p&gt;And it's really enforced, not just declared. Here's a small program that tries to write into its own &lt;code&gt;.dynamic&lt;/code&gt;, which it finds through the &lt;code&gt;_DYNAMIC&lt;/code&gt; symbol (the linker adds it to every dynamically linked binary):&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// relro_write.c&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;_DYNAMIC&lt;/span&gt;&lt;span class="p"&gt;[];&lt;/span&gt; &lt;span class="c1"&gt;// the start of .dynamic, an array of ElfW(Dyn)&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;volatile&lt;/span&gt; &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;_DYNAMIC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;.dynamic (_DYNAMIC) @ %p, first word = 0x%lx&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;attempting to overwrite .dynamic[0] ...&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;fflush&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stdout&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0x4141414141414141UL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// with RELRO this page is read-only, so SIGSEGV here&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;WRITE SUCCEEDED -&amp;gt; .dynamic is writable (no RELRO)&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;With partial RELRO the write dies with &lt;code&gt;SIGSEGV&lt;/code&gt;, and with RELRO turned off the same write goes through:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; gcc -O0 -fno-stack-protector relro_write.c -Wl,-z,relro,-z,lazy -o relro_partial &lt;span class="c1"&gt;# partial RELRO&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; gcc -O0 -fno-stack-protector relro_write.c -Wl,-z,norelro -o relro_norelro &lt;span class="c1"&gt;# RELRO off&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; ./relro_norelro&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;.dynamic (_DYNAMIC) @ 0x556b71fdc1a0, first word = 0x1&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;attempting to overwrite .dynamic[0] ...&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;WRITE SUCCEEDED -&amp;gt; .dynamic is writable (no RELRO)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$?&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; ./relro_partial&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;.dynamic (_DYNAMIC) @ 0x557e76949de0, first word = 0x1&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;attempting to overwrite .dynamic[0] ...&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Segmentation fault (core dumped)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$?&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;139&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;The exit code &lt;code&gt;139&lt;/code&gt; is 128 + 11, where 11 is the number of &lt;code&gt;SIGSEGV&lt;/code&gt;. The program writes to &lt;code&gt;.dynamic&lt;/code&gt; here, but &lt;code&gt;.init_array&lt;/code&gt;, &lt;code&gt;.fini_array&lt;/code&gt; and &lt;code&gt;.got&lt;/code&gt; are in the same &lt;code&gt;PT_GNU_RELRO&lt;/code&gt; range, and the loader makes the whole range read-only with one &lt;code&gt;mprotect&lt;/code&gt;, so a write to any of them would die the same way.&lt;/p&gt;&#10;&lt;h2 id="full-relro-resolve-everything-up-front-then-freeze-it"&gt;Full RELRO: resolve everything up front, then freeze it&lt;/h2&gt;&#10;&lt;p&gt;So partial RELRO protects almost everything except the function slots: the &lt;code&gt;puts&lt;/code&gt; slot in our &lt;code&gt;demo_partial&lt;/code&gt; stays writable for the whole life of the process, because lazy binding has to write there on the first call. In an average binary there are many more slots, but the idea is the same. Full RELRO (&lt;code&gt;-z relro -z now&lt;/code&gt;) fixes it in the most direct way: it gives up lazy binding and resolves every function at startup. After that, nothing needs to write into the GOT anymore, so the function slots can be read-only too. Here is what changes for that, in the binary and in the loader:&lt;/p&gt;&#10;&lt;p&gt;The first change is in &lt;code&gt;.dynamic&lt;/code&gt;: &lt;code&gt;readelf -d&lt;/code&gt; shows the &lt;code&gt;BIND_NOW&lt;/code&gt; bit in &lt;code&gt;FLAGS&lt;/code&gt; and the &lt;code&gt;NOW&lt;/code&gt; bit in &lt;code&gt;FLAGS_1&lt;/code&gt;, and the partial binary has neither. There's no separate &lt;code&gt;DT_BIND_NOW&lt;/code&gt; entry, just these two bits, but glibc &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/get-dynamic-info.h#L158-L159" target="_blank" rel="noopener"&gt;treats&lt;/a&gt; &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/get-dynamic-info.h#L180-L181" target="_blank" rel="noopener"&gt;both of them&lt;/a&gt; as &lt;code&gt;DT_BIND_NOW&lt;/code&gt;, which is the name I'll use below:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; gcc -O0 -fno-stack-protector -fcf-protection&lt;span class="o"&gt;=&lt;/span&gt;none -o demo_full demo.c -Wl,-z,relro,-z,now &lt;span class="c1"&gt;# full RELRO: add -z now&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -d demo_full &lt;span class="p"&gt;|&lt;/span&gt; grep -Ei &lt;span class="s1"&gt;&amp;#39;bind|flags&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x000000000000001e (FLAGS) BIND_NOW&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x000000006ffffffb (FLAGS_1) Flags: NOW PIE&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -d demo_partial &lt;span class="p"&gt;|&lt;/span&gt; grep -Ei &lt;span class="s1"&gt;&amp;#39;bind|flags&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x000000006ffffffb (FLAGS_1) Flags: PIE&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;The second change is in the loader itself. When it sees &lt;code&gt;DT_BIND_NOW&lt;/code&gt;, &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-reloc.c#L246-L248" target="_blank" rel="noopener"&gt;&lt;code&gt;_dl_relocate_object&lt;/code&gt; sets &lt;code&gt;lazy = 0&lt;/code&gt;&lt;/a&gt;, and this one variable changes two things. First, &lt;code&gt;elf_machine_runtime_setup&lt;/code&gt; doesn't fill &lt;code&gt;GOT[1]&lt;/code&gt; and &lt;code&gt;GOT[2]&lt;/code&gt; anymore, because of the same &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/sysdeps/x86_64/dl-machine.h#L78" target="_blank" rel="noopener"&gt;&lt;code&gt;lazy&lt;/code&gt; check&lt;/a&gt; we saw above: nobody will ever call the resolver in this case, so it doesn't need a &lt;code&gt;link_map&lt;/code&gt; or a trampoline address. Second, the relocation loop skips the &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/do-rel.h#L80" target="_blank" rel="noopener"&gt;cheap lazy path&lt;/a&gt; and processes every &lt;code&gt;JUMP_SLOT&lt;/code&gt; right away with &lt;code&gt;elf_machine_rela&lt;/code&gt;, the same function that handles &lt;code&gt;GLOB_DAT&lt;/code&gt;. It looks up the symbol with the same &lt;code&gt;_dl_lookup_symbol_x&lt;/code&gt; that &lt;code&gt;_dl_fixup&lt;/code&gt; uses, and &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/sysdeps/x86_64/dl-machine.h#L314-L318" target="_blank" rel="noopener"&gt;writes the address straight into the slot&lt;/a&gt;, just like on the &lt;code&gt;GLOB_DAT&lt;/code&gt; path. So by the time &lt;code&gt;main&lt;/code&gt; runs, every function slot already holds its final libc address. One more pwndbg session to confirm all this:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;# full RELRO: stop at main, before any libc call, and read the GOT&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; b main&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Breakpoint 1 at 0x113d&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; r&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Breakpoint 1, 0x000055555555513d in main ()&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;# plain got hides read-only entries, and here all of them are read-only&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; got&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Filtering out read-only entries (display them with -r or --show-readonly)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;State of the GOT of /work/demo_full:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;GOT protection: Full RELRO | Found 0 GOT entries passing the filter&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;# so show them all with -r&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; got -r&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;State of the GOT of /work/demo_full:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;GOT protection: Full RELRO | Found 6 GOT entries passing the filter&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;[0x555555557fd0] puts@GLIBC_2.2.5 -&amp;gt; 0x7ffff7e46300 (puts) ◂— push r14&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;[0x555555557fd8] __libc_start_main@GLIBC_2.34 -&amp;gt; 0x7ffff7defce0 (__libc_start_main) ◂— push r15&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;[0x555555557fe0] _ITM_deregisterTMCloneTable -&amp;gt; 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;[0x555555557fe8] __gmon_start__ -&amp;gt; 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;[0x555555557ff0] _ITM_registerTMCloneTable -&amp;gt; 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;[0x555555557ff8] __cxa_finalize@GLIBC_2.2.5 -&amp;gt; 0x7ffff7e07b90 (__cxa_finalize) ◂— push r15&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;The &lt;code&gt;puts&lt;/code&gt; slot already holds &lt;code&gt;0x7ffff7e46300&lt;/code&gt; (its real address in libc) right at the &lt;code&gt;main&lt;/code&gt; breakpoint, before any code there runs. There's no lazy stub and no slow first call, because the loader resolved the whole &lt;code&gt;.rela.plt&lt;/code&gt; in advance. And pwndbg's &lt;code&gt;got&lt;/code&gt; shows zero entries: it hides read-only slots by default, and with full RELRO every slot in the GOT is read-only. That's why you need &lt;code&gt;got -r&lt;/code&gt; here. On top of that, pwndbg also shows the RELRO level in its protection line, in case you somehow missed it: &lt;code&gt;GOT protection: Full RELRO&lt;/code&gt;.&lt;/p&gt;&#10;&lt;p&gt;The third change is in the layout: &lt;code&gt;.got.plt&lt;/code&gt; disappears. With &lt;code&gt;-z now&lt;/code&gt;, ld uses a different default linker script (compare &lt;code&gt;ld --verbose&lt;/code&gt; with &lt;code&gt;ld -z now --verbose&lt;/code&gt;), where the output &lt;code&gt;.got&lt;/code&gt; takes the &lt;code&gt;.got.plt&lt;/code&gt; input first and then the usual &lt;code&gt;.got&lt;/code&gt; entries. So all the slots end up in one &lt;code&gt;.got&lt;/code&gt;, and the whole section is inside the RELRO range:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -SW demo_full &lt;span class="p"&gt;|&lt;/span&gt; grep &lt;span class="s1"&gt;&amp;#39;\.got&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; [13] .plt.got PROGBITS 0000000000001040 001040 000008 08 AX 0 0 8&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; [23] .got PROGBITS 0000000000003fb8 002fb8 000048 08 WA 0 0 8&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -SW demo_partial &lt;span class="p"&gt;|&lt;/span&gt; grep &lt;span class="s1"&gt;&amp;#39;\.got&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; [13] .plt.got PROGBITS 0000000000001040 001040 000008 08 AX 0 0 8&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; [23] .got PROGBITS 0000000000003fc0 002fc0 000028 08 WA 0 0 8&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; [24] .got.plt PROGBITS 0000000000003fe8 002fe8 000020 08 WA 0 0 8&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;&lt;code&gt;.plt.got&lt;/code&gt; (&lt;code&gt;[13]&lt;/code&gt;) shows up only because &lt;code&gt;grep&lt;/code&gt; matches it too: it's the same in both binaries, has nothing to do with RELRO, and gets its own section further down. The real difference is &lt;code&gt;.got.plt&lt;/code&gt;: &lt;code&gt;demo_partial&lt;/code&gt; has it as a separate section at &lt;code&gt;0x3fe8&lt;/code&gt;, and &lt;code&gt;demo_full&lt;/code&gt; doesn't have it at all. The sizes add up too: the &lt;code&gt;.got&lt;/code&gt; of &lt;code&gt;demo_full&lt;/code&gt; is &lt;code&gt;0x48&lt;/code&gt; bytes, which is &lt;code&gt;0x28 + 0x20&lt;/code&gt; from &lt;code&gt;demo_partial&lt;/code&gt;.&lt;/p&gt;&#10;&lt;p&gt;The reserved &lt;code&gt;GOT[0..2]&lt;/code&gt; are still there, at the start of the merged &lt;code&gt;.got&lt;/code&gt; (&lt;code&gt;0x3fb8&lt;/code&gt; to &lt;code&gt;0x3fd0&lt;/code&gt;). The linker still emits &lt;code&gt;PLT0&lt;/code&gt; and the &lt;code&gt;puts&lt;/code&gt; stub that refer to them, but in a normal run it's all dead code: the &lt;code&gt;puts&lt;/code&gt; slot already points into libc, so the stub never gets past its first &lt;code&gt;jmp&lt;/code&gt;. All this looks like someone forgot an &lt;code&gt;if&lt;/code&gt; somewhere, but no: glibc &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-reloc.c#L243-L248" target="_blank" rel="noopener"&gt;ignores &lt;code&gt;DT_BIND_NOW&lt;/code&gt;&lt;/a&gt; when you &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/rtld.c#L2228" target="_blank" rel="noopener"&gt;profile the program with &lt;code&gt;LD_PROFILE&lt;/code&gt;&lt;/a&gt; or &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-reloc.c#L228-L231" target="_blank" rel="noopener"&gt;use an audit module with PLT hooks&lt;/a&gt;, and then this &amp;quot;dead&amp;quot; lazy path comes back to life. Anyway, &lt;code&gt;readelf -r&lt;/code&gt; lists every fixup the loader will apply, and in &lt;code&gt;.got&lt;/code&gt; they all start at &lt;code&gt;0x3fd0&lt;/code&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -r demo_full&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Relocation section &amp;#39;.rela.dyn&amp;#39; at offset 0x550 contains 8 entries:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Offset Info Type Sym. Value Sym. Name &amp;#43; Addend&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;000000003db8 000000000008 R_X86_64_RELATIVE 1130&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;000000003dc0 000000000008 R_X86_64_RELATIVE 10f0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;000000004008 000000000008 R_X86_64_RELATIVE 4008&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;000000003fd8 000100000006 R_X86_64_GLOB_DAT 0000000000000000 __libc_start_main@GLIBC_2.34 &amp;#43; 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;000000003fe0 000200000006 R_X86_64_GLOB_DAT 0000000000000000 _ITM_deregisterTM[...] &amp;#43; 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;000000003fe8 000400000006 R_X86_64_GLOB_DAT 0000000000000000 __gmon_start__ &amp;#43; 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;000000003ff0 000500000006 R_X86_64_GLOB_DAT 0000000000000000 _ITM_registerTMCl[...] &amp;#43; 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;000000003ff8 000600000006 R_X86_64_GLOB_DAT 0000000000000000 __cxa_finalize@GLIBC_2.2.5 &amp;#43; 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Relocation section &amp;#39;.rela.plt&amp;#39; at offset 0x610 contains 1 entry:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Offset Info Type Sym. Value Sym. Name &amp;#43; Addend&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;000000003fd0 000300000007 R_X86_64_JUMP_SLO 0000000000000000 puts@GLIBC_2.2.5 &amp;#43; 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;If you run the same &lt;code&gt;readelf -r&lt;/code&gt; on &lt;code&gt;demo_partial&lt;/code&gt;, you'll get the same set of relocations: three &lt;code&gt;R_X86_64_RELATIVE&lt;/code&gt;, five &lt;code&gt;R_X86_64_GLOB_DAT&lt;/code&gt; and one &lt;code&gt;R_X86_64_JUMP_SLOT&lt;/code&gt; for &lt;code&gt;puts&lt;/code&gt;. So the relocation table doesn't say anything about RELRO. The only difference is where the &lt;code&gt;JUMP_SLOT&lt;/code&gt; points: &lt;code&gt;0x3fd0&lt;/code&gt; in &lt;code&gt;demo_full&lt;/code&gt;, inside &lt;code&gt;.got&lt;/code&gt; and below the page boundary, and &lt;code&gt;0x4000&lt;/code&gt; in &lt;code&gt;demo_partial&lt;/code&gt;, the first byte of the next, writable page.&lt;/p&gt;&#10;&lt;p&gt;The reserved &lt;code&gt;GOT[0..2]&lt;/code&gt; at &lt;code&gt;0x3fb8&lt;/code&gt;, &lt;code&gt;0x3fc0&lt;/code&gt; and &lt;code&gt;0x3fc8&lt;/code&gt; aren't in either list. The lowest &lt;code&gt;.got&lt;/code&gt; offset there is &lt;code&gt;0x3fd0&lt;/code&gt; (the &lt;code&gt;puts&lt;/code&gt; slot), the &lt;code&gt;GLOB_DAT&lt;/code&gt;s go from &lt;code&gt;0x3fd8&lt;/code&gt; up, and the three &lt;code&gt;R_X86_64_RELATIVE&lt;/code&gt; relocations (&lt;code&gt;0x3db8&lt;/code&gt;, &lt;code&gt;0x3dc0&lt;/code&gt; and &lt;code&gt;0x4008&lt;/code&gt;) aren't in &lt;code&gt;.got&lt;/code&gt; at all. So no relocation touches &lt;code&gt;GOT[1]&lt;/code&gt; and &lt;code&gt;GOT[2]&lt;/code&gt;, the loader skips them too (as we saw above), and at &lt;code&gt;main&lt;/code&gt; they are still zero:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; x/3gx $rebase(0x3fb8) # GOT[0..2] at the start of .got (0x3fb8 in readelf -S above)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0x555555557fb8:&#9;0x0000000000003dc8&#9;0x0000000000000000&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0x555555557fc8:&#9;0x0000000000000000&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;&lt;code&gt;GOT[0]&lt;/code&gt; still has the raw link-time &lt;code&gt;0x3dc8&lt;/code&gt; (the address of &lt;code&gt;_DYNAMIC&lt;/code&gt;, not even rebased), and &lt;code&gt;GOT[1]&lt;/code&gt; and &lt;code&gt;GOT[2]&lt;/code&gt; are zero, while the function slots right after them are already resolved (see the &lt;code&gt;got -r&lt;/code&gt; output above). So in the end, the reserved slots are still there, only &lt;code&gt;PLT0&lt;/code&gt; refers to them, and in a normal run nobody uses them.&lt;/p&gt;&#10;&lt;p&gt;So the call logic doesn't change at all: &lt;code&gt;main&lt;/code&gt; still does &lt;code&gt;call puts@plt&lt;/code&gt;, and the stub still jumps through the GOT slot. What changes is when the slot gets its address, and that it's secured so nobody can write there after startup. That's the trade full RELRO makes: the program pays for resolving everything at startup, and in exchange an attacker loses a writable table of function pointers, which is a very obvious thing to overwrite. More on that in &lt;a href="#the-got-overwrite-primitive-and-what-full-relro-does-to-it"&gt;the last section&lt;/a&gt;.&lt;/p&gt;&#10;&lt;h2 id="taking-the-address-eager-functions-and-pltgot"&gt;Taking the address: eager functions and &lt;code&gt;.plt.got&lt;/code&gt;&lt;/h2&gt;&#10;&lt;p&gt;So far I experimented with a function (&lt;code&gt;puts&lt;/code&gt;) that was only called, and that's why it could be bound lazily: the call goes through the PLT stub, and the first call fills its &lt;code&gt;.got.plt&lt;/code&gt; slot. But what if the program takes the address of &lt;code&gt;puts&lt;/code&gt;, i.e. stores &lt;code&gt;&amp;amp;puts&lt;/code&gt; in a function pointer, compares it with another one or passes it somewhere as a callback? In this case, this address is just a value read as data, so it has to be the real address of &lt;code&gt;puts&lt;/code&gt; from the very start, without running any lazy machinery. In a PIE, the address of the PLT stub can't play this role: &lt;code&gt;&amp;amp;puts&lt;/code&gt; taken in the executable would then differ from &lt;code&gt;&amp;amp;puts&lt;/code&gt; taken inside a shared library, and comparing function pointers between objects would silently break. So the linker has to bind such a function eagerly: the loader fills a single &lt;code&gt;.got&lt;/code&gt; slot at startup through a &lt;code&gt;GLOB_DAT&lt;/code&gt; relocation, and the code reads &lt;code&gt;&amp;amp;puts&lt;/code&gt; from this slot.&lt;/p&gt;&#10;&lt;p&gt;As a side note, a non-PIE executable (compiled with &lt;code&gt;-fno-pie&lt;/code&gt;) does the opposite trick. Its PLT stub has a fixed address, so the linker makes this stub the official (but still fake, because it's not in libc) address of &lt;code&gt;puts&lt;/code&gt; for the whole process. And the loader gives the same address to every shared library that asks for &lt;code&gt;&amp;amp;puts&lt;/code&gt;. But every binary in this post is a PIE, so no demo here: just believe me, or better, check it yourself.&lt;/p&gt;&#10;&lt;p&gt;The funny thing is that once the code takes the address of &lt;code&gt;puts&lt;/code&gt; through the GOT, the call starts to use the same eager slot too. The compiler loads &lt;code&gt;&amp;amp;puts&lt;/code&gt; from a GOT slot (&lt;code&gt;puts@GOTPCREL&lt;/code&gt;), the linker emits a &lt;code&gt;GLOB_DAT&lt;/code&gt; relocation for this slot, and then redirects the &lt;code&gt;call&lt;/code&gt; through the same slot with a tiny &lt;code&gt;.plt.got&lt;/code&gt; stub instead of a standard lazy &lt;code&gt;.plt&lt;/code&gt; entry. So such a function requires no &lt;code&gt;JUMP_SLOT&lt;/code&gt; at all. Here's &lt;code&gt;addr.c&lt;/code&gt;, which takes &lt;code&gt;&amp;amp;puts&lt;/code&gt; in two ways and also calls it:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; cat addr.c&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;#include &amp;lt;stdio.h&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;void *p = (void *)&amp;amp;puts; /* address stored in .data -&amp;gt; R_X86_64_64 */&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;int main(void){&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; void *volatile q = (void *)&amp;amp;puts; /* address loaded via the GOT -&amp;gt; GLOB_DAT */&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; puts(&amp;#34;hi&amp;#34;); /* called too */&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; return (p != 0) &amp;amp;&amp;amp; (q != 0);&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;}&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; gcc -O0 -fno-stack-protector -fcf-protection&lt;span class="o"&gt;=&lt;/span&gt;none -o addr addr.c&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -r addr &lt;span class="p"&gt;|&lt;/span&gt; grep puts&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;000000003fc8 000600000006 R_X86_64_GLOB_DAT 0000000000000000 puts@GLIBC_2.2.5 &amp;#43; 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;000000004010 000600000001 R_X86_64_64 0000000000000000 puts@GLIBC_2.2.5 &amp;#43; 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -S addr &lt;span class="p"&gt;|&lt;/span&gt; grep -c rela.plt&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;So there are two eager relocations for &lt;code&gt;puts&lt;/code&gt;. The &lt;code&gt;GLOB_DAT&lt;/code&gt; at &lt;code&gt;0x3fc8&lt;/code&gt; is the GOT slot with the one real address of &lt;code&gt;puts&lt;/code&gt; (this is what &lt;code&gt;q = &amp;amp;puts&lt;/code&gt; reads), and the &lt;code&gt;R_X86_64_64&lt;/code&gt; at &lt;code&gt;0x4010&lt;/code&gt; writes the same address straight into &lt;code&gt;p&lt;/code&gt; in &lt;code&gt;.data&lt;/code&gt;. The loader applies both of them at startup, and &lt;code&gt;.rela.plt&lt;/code&gt; doesn't even exist (that's the &lt;code&gt;0&lt;/code&gt; above), so &lt;code&gt;puts&lt;/code&gt; has no lazy &lt;code&gt;JUMP_SLOT&lt;/code&gt;.&lt;/p&gt;&#10;&lt;p&gt;The call is eager too. &lt;code&gt;main&lt;/code&gt; does an ordinary-looking &lt;code&gt;call 1030 &amp;lt;puts@plt&amp;gt;&lt;/code&gt;, but &lt;code&gt;0x1030&lt;/code&gt; isn't in &lt;code&gt;.plt&lt;/code&gt;, it's a &lt;code&gt;.plt.got&lt;/code&gt; stub that jumps straight through the same &lt;code&gt;GLOB_DAT&lt;/code&gt; slot:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; objdump -d -M intel addr&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; ...&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1131:&#9;48 8b 05 90 2e 00 00 &#9;mov rax,QWORD PTR [rip&amp;#43;0x2e90] # 3fc8 &amp;lt;puts@GLIBC_2.2.5&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; ...&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1146:&#9;e8 e5 fe ff ff &#9;call 1030 &amp;lt;puts@plt&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; objdump -d -M intel -j .plt.got addr&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0000000000001030 &amp;lt;puts@plt&amp;gt;:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1030:&#9;ff 25 92 2f 00 00 &#9;jmp QWORD PTR [rip&amp;#43;0x2f92] # 3fc8 &amp;lt;puts@GLIBC_2.2.5&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1036:&#9;66 90 &#9;xchg ax,ax&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;So both &lt;code&gt;mov&lt;/code&gt; and &lt;code&gt;call&lt;/code&gt; end up reading &lt;code&gt;0x3fc8&lt;/code&gt;. Compare it with &lt;code&gt;demo_partial&lt;/code&gt;, where &lt;code&gt;puts&lt;/code&gt; is only called: there it has a lazy &lt;code&gt;JUMP_SLOT&lt;/code&gt; in &lt;code&gt;.rela.plt&lt;/code&gt; and an ordinary &lt;code&gt;.plt&lt;/code&gt; stub, because nobody takes its address.&lt;/p&gt;&#10;&lt;p&gt;But you don't even need to bother writing &lt;code&gt;&amp;amp;&lt;/code&gt; in your own code if you want to get a &lt;code&gt;.plt.got&lt;/code&gt; stub: every ordinary binary has one, for the &lt;code&gt;__cxa_finalize&lt;/code&gt; function. Here it is in &lt;code&gt;demo_partial&lt;/code&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; objdump -d -M intel -j .plt.got demo_partial&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;...&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0000000000001040 &amp;lt;__cxa_finalize@plt&amp;gt;:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1040:&#9;ff 25 9a 2f 00 00 &#9;jmp QWORD PTR [rip&amp;#43;0x2f9a] # 3fe0 &amp;lt;__cxa_finalize@GLIBC_2.2.5&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1046:&#9;66 90 &#9;xchg ax,ax&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -r demo_partial &lt;span class="p"&gt;|&lt;/span&gt; grep cxa&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;000000003fe0 000600000006 R_X86_64_GLOB_DAT 0000000000000000 __cxa_finalize@GLIBC_2.2.5 &amp;#43; 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;The reason is the same: the address is taken, just not by you. The startup code that gcc links into every binary declares &lt;code&gt;__cxa_finalize&lt;/code&gt; as a &lt;a href="https://github.com/gcc-mirror/gcc/blob/releases/gcc-14.2.0/libgcc/crtstuff.c#L351-L353" target="_blank" rel="noopener"&gt;weak reference&lt;/a&gt; and &lt;a href="https://github.com/gcc-mirror/gcc/blob/releases/gcc-14.2.0/libgcc/crtstuff.c#L388-L389" target="_blank" rel="noopener"&gt;checks it before the call&lt;/a&gt; with &lt;code&gt;if (__cxa_finalize)&lt;/code&gt;, which reads its address. So the result is the same as in my example: one &lt;code&gt;GLOB_DAT&lt;/code&gt; slot in &lt;code&gt;.got&lt;/code&gt;, filled at startup, and a &lt;code&gt;.plt.got&lt;/code&gt; stub for the call. Here is the rule of thumb if you need one: a &lt;code&gt;.plt.got&lt;/code&gt; entry means a &lt;code&gt;GLOB_DAT&lt;/code&gt; relocation in &lt;code&gt;.rela.dyn&lt;/code&gt;, and a &lt;code&gt;.plt&lt;/code&gt; (or &lt;code&gt;.plt.sec&lt;/code&gt;) entry means a &lt;code&gt;JUMP_SLOT&lt;/code&gt; in &lt;code&gt;.rela.plt&lt;/code&gt;.&lt;/p&gt;&#10;&lt;h2 id="pltsec-what-cet-does-to-the-plt"&gt;&lt;code&gt;.plt.sec&lt;/code&gt;: what CET does to the PLT&lt;/h2&gt;&#10;&lt;p&gt;Every binary above was built with &lt;code&gt;-fcf-protection=none&lt;/code&gt;, so they all had the classic layout with a single &lt;code&gt;.plt&lt;/code&gt;. Now it's time to turn CET on and see what changes.&lt;/p&gt;&#10;&lt;p&gt;Intel CET (Control-flow Enforcement Technology) is a hardware protection against control-flow hijacking, like &lt;a href="https://en.wikipedia.org/wiki/Return-oriented_programming" target="_blank" rel="noopener"&gt;ROP&lt;/a&gt; and &lt;a href="https://security.stackexchange.com/questions/201196/concept-of-jump-oriented-programming-jop" target="_blank" rel="noopener"&gt;JOP&lt;/a&gt;. It has two parts: the shadow stack protects return addresses (a second &amp;quot;invisible&amp;quot; copy of them that the CPU checks on every &lt;code&gt;ret&lt;/code&gt;) and has nothing to do with the PLT, and the one that matters here is &lt;a href="https://en.wikipedia.org/wiki/Indirect_branch_tracking" target="_blank" rel="noopener"&gt;IBT&lt;/a&gt; (Indirect Branch Tracking). With IBT, the CPU checks every indirect &lt;code&gt;call&lt;/code&gt; and &lt;code&gt;jmp&lt;/code&gt; and raises a control-protection fault (&lt;code&gt;#CP&lt;/code&gt;) unless the first instruction at the target is &lt;code&gt;endbr64&lt;/code&gt;. This &lt;code&gt;endbr64&lt;/code&gt; (&lt;code&gt;f3 0f 1e fa&lt;/code&gt;) is a 4-byte marker that says &amp;quot;an indirect branch is allowed to land here&amp;quot;. It's encoded as a multi-byte &lt;code&gt;NOP&lt;/code&gt;, so a CPU without CET just skips it, and the same binary runs everywhere. You can turn it on with the &lt;code&gt;-fcf-protection&lt;/code&gt; flag.&lt;/p&gt;&#10;&lt;p&gt;And that's what breaks the classic stub: the first jump through the GOT lands at &lt;code&gt;0x1036&lt;/code&gt;, in the middle of the stub, where there's no &lt;code&gt;endbr64&lt;/code&gt; instruction. So with IBT enabled it would fault and kill the process. To fix this, the linker splits the PLT into two parts. Here's the same &lt;code&gt;demo.c&lt;/code&gt; built with CET (I'll explain the &lt;code&gt;-z ibt&lt;/code&gt; part right after the listing):&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; gcc -O0 -fno-stack-protector -fcf-protection&lt;span class="o"&gt;=&lt;/span&gt;branch -o demo_cet demo.c -Wl,-z,relro,-z,lazy,-z,ibt&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; objdump -d -M intel -j .plt -j .plt.sec demo_cet&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Disassembly of section .plt:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0000000000001020 &amp;lt;.plt&amp;gt;:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1020:&#9;ff 35 ca 2f 00 00 &#9;push QWORD PTR [rip&amp;#43;0x2fca] # 3ff0 &amp;lt;_GLOBAL_OFFSET_TABLE_&amp;#43;0x8&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1026:&#9;ff 25 cc 2f 00 00 &#9;jmp QWORD PTR [rip&amp;#43;0x2fcc] # 3ff8 &amp;lt;_GLOBAL_OFFSET_TABLE_&amp;#43;0x10&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 102c:&#9;0f 1f 40 00 &#9;nop DWORD PTR [rax&amp;#43;0x0]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1030:&#9;f3 0f 1e fa &#9;endbr64&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1034:&#9;68 00 00 00 00 &#9;push 0x0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1039:&#9;e9 e2 ff ff ff &#9;jmp 1020 &amp;lt;_init&amp;#43;0x20&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 103e:&#9;66 90 &#9;xchg ax,ax&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Disassembly of section .plt.sec:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0000000000001050 &amp;lt;puts@plt&amp;gt;:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1050:&#9;f3 0f 1e fa &#9;endbr64&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1054:&#9;ff 25 a6 2f 00 00 &#9;jmp QWORD PTR [rip&amp;#43;0x2fa6] # 4000 &amp;lt;puts@GLIBC_2.2.5&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 105a:&#9;66 0f 1f 44 00 00 &#9;nop WORD PTR [rax&amp;#43;rax*1&amp;#43;0x0]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -n demo_cet &lt;span class="p"&gt;|&lt;/span&gt; grep -i feature&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Properties: x86 feature: IBT&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;Now about &lt;code&gt;-z ibt&lt;/code&gt;. The linker emits &lt;code&gt;.plt.sec&lt;/code&gt; only when the whole output is marked as IBT-compatible, whatever that means. This mark is a logical AND over the &lt;code&gt;.note.gnu.property&lt;/code&gt; of every input file: your object files and the required C runtime startup files that the distro ships and you link with (&lt;code&gt;Scrt1.o&lt;/code&gt;, &lt;code&gt;crti.o&lt;/code&gt; and others). &lt;code&gt;-fcf-protection=branch&lt;/code&gt; marks only your own code. So if the distro's startup files aren't marked, the AND clears the IBT bit, and you get the classic single &lt;code&gt;.plt&lt;/code&gt; without &lt;code&gt;.plt.sec&lt;/code&gt;. That's what happens on my Debian: &lt;code&gt;Scrt1.o&lt;/code&gt;, &lt;code&gt;crti.o&lt;/code&gt; and &lt;code&gt;crtn.o&lt;/code&gt; have no IBT mark (check yours with &lt;code&gt;readelf -n $(gcc -print-file-name=Scrt1.o)&lt;/code&gt;). On Ubuntu and Fedora (at least 24.04 and 41) these files are marked, so a plain &lt;code&gt;-fcf-protection=branch&lt;/code&gt; is enough there. My &lt;code&gt;-z ibt&lt;/code&gt; from the example just forces the IBT bit on the output, and the CET layout of the PLT with it, no matter what the inputs say.&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;main&lt;/code&gt; now does &lt;code&gt;call 1050 &amp;lt;puts@plt&amp;gt;&lt;/code&gt;, which lands in &lt;code&gt;.plt.sec&lt;/code&gt;. A &lt;code&gt;.plt.sec&lt;/code&gt; stub is just &lt;code&gt;endbr64&lt;/code&gt; and a &lt;code&gt;jmp&lt;/code&gt; through the already familiar GOT slot, and the old &lt;code&gt;.plt&lt;/code&gt; keeps &lt;code&gt;PLT0&lt;/code&gt; and the lazy stubs. The &lt;code&gt;puts&lt;/code&gt; slot in the GOT initially points to the &lt;code&gt;endbr64&lt;/code&gt; of its lazy stub at &lt;code&gt;0x1030&lt;/code&gt; to make IBT happy. In the classic layout it pointed to &lt;code&gt;0x1036&lt;/code&gt;, the &lt;code&gt;push&lt;/code&gt; in the middle of the stub, but now the jump has to land on an &lt;code&gt;endbr64&lt;/code&gt; instruction. So &lt;code&gt;.plt.sec&lt;/code&gt; is what the code calls, and &lt;code&gt;.plt&lt;/code&gt; is still the way to the resolver. The first call goes &lt;code&gt;.plt.sec&lt;/code&gt; -&amp;gt; GOT -&amp;gt; lazy stub in &lt;code&gt;.plt&lt;/code&gt; -&amp;gt; &lt;code&gt;PLT0&lt;/code&gt; -&amp;gt; resolver, and after that the slot points straight to libc as usual, so &lt;code&gt;.plt.sec&lt;/code&gt; needs just one jump. Same mechanism, one more section, one more stub, one more abstraction, security folks are happy.&lt;/p&gt;&#10;&lt;h2 id="gnuhash-vs-hash-finding-puts-in-libc-fast"&gt;&lt;code&gt;.gnu.hash&lt;/code&gt; vs &lt;code&gt;.hash&lt;/code&gt;: finding &lt;code&gt;puts&lt;/code&gt; in libc fast&lt;/h2&gt;&#10;&lt;p&gt;As promised back in &lt;a href="#the-resolving-logic-itself"&gt;the resolving part&lt;/a&gt; of the post, here's how &lt;code&gt;do_lookup_x&lt;/code&gt; does its lightning-fast searches inside one object. The loader asks every object in the search scope &amp;quot;do you have &lt;code&gt;puts&lt;/code&gt;?&amp;quot;, and almost all of them answer &amp;quot;no&amp;quot;. And they have to answer fast: my libc alone exports about 3100 symbols, and comparing the name against each of them with &lt;code&gt;strcmp&lt;/code&gt;, for every lookup, would be painfully slow even with all the assembly tricks inside it. That's what the hash sections are for.&lt;/p&gt;&#10;&lt;p&gt;There are two of them. The old one is &lt;code&gt;.hash&lt;/code&gt; (&lt;code&gt;DT_HASH&lt;/code&gt;), a plain hash table with chains, which is part of the standard System V ABI. The newer one is &lt;code&gt;.gnu.hash&lt;/code&gt; (&lt;code&gt;DT_GNU_HASH&lt;/code&gt;), a GNU extension from around 2006, and that's what GNU ld produces by default now, at least on Debian (&lt;code&gt;ld --help&lt;/code&gt; says &amp;quot;Default: gnu&amp;quot;, and we just have to believe in GNU). You get the old &lt;code&gt;.hash&lt;/code&gt; only if you specifically ask for it with &lt;code&gt;--hash-style=sysv&lt;/code&gt; or &lt;code&gt;--hash-style=both&lt;/code&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; gcc demo.c -o demo &lt;span class="c1"&gt;# only GNU_HASH by default&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -d demo &lt;span class="p"&gt;|&lt;/span&gt; grep -i hash&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x000000006ffffef5 (GNU_HASH) 0x3b0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; gcc demo.c -o demo -Wl,--hash-style&lt;span class="o"&gt;=&lt;/span&gt;both &lt;span class="c1"&gt;# both of them&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -d demo &lt;span class="p"&gt;|&lt;/span&gt; grep -i hash&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x0000000000000004 (HASH) 0x3b0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x000000006ffffef5 (GNU_HASH) 0x3e0&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;The main point of adding &lt;code&gt;.gnu.hash&lt;/code&gt; (besides the fact that GNU usually likes adding something on top of the standard) is that it's built to make the &amp;quot;no&amp;quot; cheap, because that's the answer the symbol search gives most of the time. A plain hash table can't do that: even for a missing name, the bucket it lands in may hold a chain of other symbols, and the loader has to walk through it using the same &lt;code&gt;strcmp&lt;/code&gt;. So &lt;code&gt;.gnu.hash&lt;/code&gt; smartly puts a &lt;a href="https://en.wikipedia.org/wiki/Bloom_filter" target="_blank" rel="noopener"&gt;Bloom filter&lt;/a&gt; in front of the table. It's a small bit array that can answer either &amp;quot;definitely not here&amp;quot; or &amp;quot;maybe here&amp;quot;, but never a wrong &amp;quot;no&amp;quot;. The loader &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-lookup.c#L398-L407" target="_blank" rel="noopener"&gt;takes one 64-bit word from it and checks only two bits&lt;/a&gt; that depend on the hash of the name. If either of them is 0, there's no such name in this object, and the lookup in it is over. The loader goes to the real table only if both bits are set.&lt;/p&gt;&#10;&lt;p&gt;This real table is the well-known buckets and chains, with one improvement. The resulting bucket for the calculated hash gives the &lt;code&gt;.dynsym&lt;/code&gt; index of the first candidate, and the chain array stores the hash of every symbol, so the loader &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-lookup.c#L409-L424" target="_blank" rel="noopener"&gt;compares the hashes first&lt;/a&gt; and &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-lookup.c#L89-L91" target="_blank" rel="noopener"&gt;calls &lt;code&gt;strcmp&lt;/code&gt;&lt;/a&gt; only when they match. The lowest bit of each stored hash is reused as a &amp;quot;last in this bucket&amp;quot; flag, which is why the comparison ignores it. All of this lives in one special section, after a 16-byte header with four fields: the total number of buckets (&lt;code&gt;nbuckets&lt;/code&gt;), the &lt;code&gt;.dynsym&lt;/code&gt; index of the first symbol in the table (&lt;code&gt;symndx&lt;/code&gt;, everything before it, usually the undefined imports, is never hashed). Then come the size of the guarding Bloom filter in 64-bit words (&lt;code&gt;maskwords&lt;/code&gt;) and the shift for its second bit (&lt;code&gt;shift&lt;/code&gt;). glibc &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-setup_hash.c#L32-L45" target="_blank" rel="noopener"&gt;reads them one by one&lt;/a&gt; when it loads the object. And the hash function itself is &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/sysdeps/generic/dl-new-hash.h#L26-L34" target="_blank" rel="noopener"&gt;quite simple&lt;/a&gt; (it's called djb2): start with &lt;code&gt;h = 5381&lt;/code&gt;, and for every byte of the name do &lt;code&gt;h = h * 33 + c&lt;/code&gt;. It runs only once per lookup: &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-lookup.c#L760-L779" target="_blank" rel="noopener"&gt;&lt;code&gt;_dl_lookup_symbol_x&lt;/code&gt; calculates the hash and passes it to &lt;code&gt;do_lookup_x&lt;/code&gt;&lt;/a&gt; for every object in the scope.&lt;/p&gt;&#10;&lt;p&gt;Demo time! Here's a short Python script (it needs &lt;code&gt;pyelftools&lt;/code&gt;, so &lt;code&gt;apt install python3-pyelftools&lt;/code&gt; on Debian) that repeats the same steps as the loader on the real libc:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;struct&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;sys&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;itertools&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;elftools.elf.elffile&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;ELFFile&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;elf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ELFFile&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;load_from_path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;elf&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;elfclass&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;64&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;elf&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;little_endian&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;64-bit little-endian ELF only&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;dynsym&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;elf&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_section_by_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;.dynsym&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;gnu_hash&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;elf&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_section_by_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;.gnu.hash&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;data&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# the header, then the Bloom filter, the buckets and the chains, back to back&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;nbuckets&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;symndx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;maskwords&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;shift&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;struct&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;unpack_from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&amp;lt;4I&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;gnu_hash&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;buckets_at&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;maskwords&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;chains_at&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;buckets_at&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;nbuckets&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;bloom&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;struct&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;unpack_from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&amp;lt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;maskwords&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;Q&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;gnu_hash&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;buckets&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;struct&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;unpack_from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&amp;lt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;nbuckets&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;I&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;gnu_hash&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;buckets_at&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;chains&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;struct&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;unpack_from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&amp;lt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gnu_hash&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;chains_at&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;I&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;gnu_hash&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;chains_at&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;nbuckets&lt;/span&gt;&lt;span class="si"&gt;=}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;symndx&lt;/span&gt;&lt;span class="si"&gt;=}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;maskwords&lt;/span&gt;&lt;span class="si"&gt;=}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;shift&lt;/span&gt;&lt;span class="si"&gt;=}&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;lookup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5381&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;33&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mh"&gt;0xffffffff&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;hash(&amp;#34;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s1"&gt;&amp;#34;) = &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="s1"&gt;#010x&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bloom&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="mi"&gt;64&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;maskwords&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;bit1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bit2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;shift&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;64&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;bloom word &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="mi"&gt;64&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;maskwords&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; = &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;#018x&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;, &amp;#34;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;bit &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;bit1&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; = &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;bit1&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;, bit &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;bit2&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; = &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;bit2&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;bit1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;bit2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;definitely not here&amp;#34;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;buckets&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;nbuckets&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;bucket &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;nbuckets&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; -&amp;gt; dynsym[&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;]&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;empty bucket, the Bloom filter was wrong&amp;#34;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;idx&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;stored&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;chains&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;idx&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;symndx&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;sym&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;dynsym&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_symbol&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;idx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34; dynsym[&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;idx&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;] chain &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;stored&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;#010x&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;sym&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stored&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;sym&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;found&amp;#34;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;stored&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="c1"&gt;# bit 0 marks the last symbol in this bucket&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;not in the chain, the Bloom filter was wrong&amp;#34;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;lookup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And here's what it says about &lt;code&gt;puts&lt;/code&gt; and about a name that libc definitely doesn't have:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; python3 gnuhash.py /lib/x86_64-linux-gnu/libc.so.6 puts&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;nbuckets=1009 symndx=21 maskwords=512 shift=15&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;hash(&amp;#34;puts&amp;#34;) = 0x7c9c7b11&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;bloom word 492 = 0xd588080098220002, bit 17 = 1, bit 56 = 1&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;bucket 67 -&amp;gt; dynsym[235]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; dynsym[235] chain 0x886b1bb4 iconv_open&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; dynsym[236] chain 0x7c9c7b10 puts&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;found&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; python3 gnuhash.py /lib/x86_64-linux-gnu/libc.so.6 fork_me_not&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;nbuckets=1009 symndx=21 maskwords=512 shift=15&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;hash(&amp;#34;fork_me_not&amp;#34;) = 0x0455b758&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;bloom word 221 = 0x4140150200800180, bit 24 = 0, bit 43 = 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;definitely not here&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;The &lt;code&gt;puts&lt;/code&gt; lookup goes just as expected. Both Bloom bits are set, bucket 67 starts at &lt;code&gt;dynsym[235]&lt;/code&gt;, and the first candidate there is &lt;code&gt;iconv_open&lt;/code&gt;. Its stored hash is completely different, so the loader skips it without any &lt;code&gt;strcmp&lt;/code&gt;. The next one is &lt;code&gt;puts&lt;/code&gt; at index 236, the same index &lt;code&gt;readelf --dyn-syms&lt;/code&gt; gives it. Its stored hash &lt;code&gt;0x7c9c7b10&lt;/code&gt; differs from the real &lt;code&gt;0x7c9c7b11&lt;/code&gt; only in that flag bit (0 here, so the chain goes on after &lt;code&gt;puts&lt;/code&gt;). And the famous &lt;code&gt;fork_me_not&lt;/code&gt; never gets past the Bloom filter: both of its bits are 0, so the table isn't touched at all. By the way, that's also what really happens to the executable's undefined &lt;code&gt;puts&lt;/code&gt; from &lt;a href="#the-resolving-logic-itself"&gt;the resolving part&lt;/a&gt;: in &lt;code&gt;demo_partial&lt;/code&gt;, it's &lt;code&gt;dynsym[3]&lt;/code&gt;, below its &lt;code&gt;symndx&lt;/code&gt; of 6, so the Bloom filter says &amp;quot;no&amp;quot; and &lt;code&gt;check_match&lt;/code&gt; never even sees it. In my &amp;quot;versatile&amp;quot; experiment, I passed 20000 names (&lt;code&gt;nope0&lt;/code&gt; to &lt;code&gt;nope19999&lt;/code&gt;) to the script, and the Bloom filter rejected 19437 of them (about 97%), which is quite good.&lt;/p&gt;&#10;&lt;p&gt;Why this matters for RE: this hash function is easy to recognize in a disassembly. It's &lt;code&gt;mov eax, 0x1505&lt;/code&gt; (that's 5381) followed by a loop with &lt;code&gt;shl&lt;/code&gt; by 5 and two &lt;code&gt;add&lt;/code&gt;s, or with &lt;code&gt;imul eax, eax, 0x21&lt;/code&gt; if the code was optimized for size. Of course, djb2 is used in plenty of ordinary hash tables, but if the same code also parses ELF headers and walks &lt;code&gt;.dynsym&lt;/code&gt;, it's most likely resolving symbols by itself, without the loader. Malware likes doing that too, and it gets &lt;a href="#how-malware-mimics-the-loader-dynamic-import-resolution"&gt;its own section&lt;/a&gt; a bit later.&lt;/p&gt;&#10;&lt;h2 id="no-dynamic-loader-but-still-a-plt-static-binaries-case"&gt;No dynamic loader, but still a PLT: static binaries case&lt;/h2&gt;&#10;&lt;p&gt;A fully static binary (&lt;code&gt;gcc -static&lt;/code&gt;) doesn't need the dynamic loader at all: everything, libc included, is linked into one file:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; gcc -O0 -fno-stack-protector -fcf-protection&lt;span class="o"&gt;=&lt;/span&gt;none -static -o demo_static demo.c&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -h demo_static &lt;span class="p"&gt;|&lt;/span&gt; grep Type&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Type: EXEC (Executable file)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -l demo_static &lt;span class="p"&gt;|&lt;/span&gt; grep -E &lt;span class="s1"&gt;&amp;#39;INTERP|DYNAMIC&amp;#39;&lt;/span&gt; &lt;span class="c1"&gt;# nothing&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -d demo_static&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;There is no dynamic section in this file.&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; objdump -d -M intel demo_static &lt;span class="p"&gt;|&lt;/span&gt; grep &lt;span class="s1"&gt;&amp;#39;call.*&amp;lt;_IO_puts&amp;gt;&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 401733:&#9;e8 68 31 00 00 &#9;call 4048a0 &amp;lt;_IO_puts&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 401742:&#9;e8 59 31 00 00 &#9;call 4048a0 &amp;lt;_IO_puts&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;No &lt;code&gt;PT_INTERP&lt;/code&gt; present, so the kernel doesn't even load &lt;code&gt;ld.so&lt;/code&gt;, and no &lt;code&gt;PT_DYNAMIC&lt;/code&gt; either, so there's nothing to resolve. Both &lt;code&gt;puts&lt;/code&gt; calls go straight to &lt;code&gt;_IO_puts&lt;/code&gt; (remember that libc's &lt;code&gt;puts&lt;/code&gt; is a weak alias of it) with a plain &lt;code&gt;call&lt;/code&gt;: it's an &lt;code&gt;ET_EXEC&lt;/code&gt; at a fixed address, so the linker knows exactly where everything is.&lt;/p&gt;&#10;&lt;p&gt;But surprisingly (to me), the static binary still has &lt;code&gt;.plt&lt;/code&gt;, &lt;code&gt;.got&lt;/code&gt;, &lt;code&gt;.got.plt&lt;/code&gt; and even &lt;code&gt;.rela.plt&lt;/code&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -S demo_static &lt;span class="p"&gt;|&lt;/span&gt; grep -E &lt;span class="s1"&gt;&amp;#39;\.plt|\.got&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; [ 3] .rela.plt RELA 00000000004002f0 000002f0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; [ 5] .plt PROGBITS 0000000000401018 00001018&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; [18] .got PROGBITS 00000000004a3f48 000a2f48&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; [19] .got.plt PROGBITS 00000000004a3fe8 000a2fe8&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -rW demo_static &lt;span class="p"&gt;|&lt;/span&gt; grep -oE &lt;span class="s1"&gt;&amp;#39;R_X86_64_[A-Z_]+&amp;#39;&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; sort &lt;span class="p"&gt;|&lt;/span&gt; uniq -c&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 22 R_X86_64_IRELATIVE&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;and all 22 relocations in its &lt;code&gt;.rela.plt&lt;/code&gt; are of one type, &lt;code&gt;R_X86_64_IRELATIVE&lt;/code&gt;.&lt;/p&gt;&#10;&lt;p&gt;That's how IFUNC (indirect function, another GNU extension) works. Some libc functions, like &lt;code&gt;memcpy&lt;/code&gt;, &lt;code&gt;memcmp&lt;/code&gt;, &lt;code&gt;strcmp&lt;/code&gt; and &lt;code&gt;memset&lt;/code&gt;, come in several versions optimized for different CPUs (SSE2, AVX2, AVX-512 and so on). To properly choose one, each of them has a resolver: a small function that looks at the detected CPU features and returns the address of the best version. For &lt;code&gt;memcmp&lt;/code&gt; on x86-64, the resolver's choice is made in &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/sysdeps/x86_64/multiarch/ifunc-memcmp.h#L29-L53" target="_blank" rel="noopener"&gt;&lt;code&gt;IFUNC_SELECTOR&lt;/code&gt;&lt;/a&gt;, which returns &lt;code&gt;__memcmp_evex_movbe&lt;/code&gt;, one of the two AVX2 versions, or &lt;code&gt;__memcmp_sse2&lt;/code&gt; as the fallback. The linker can't make this choice by itself, because it doesn't know which CPU the binary will run on, so the &lt;code&gt;IRELATIVE&lt;/code&gt; relocation stores the address of the resolver instead of a symbol. At startup, &lt;code&gt;__libc_start_main&lt;/code&gt; first &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/csu/libc-start.c#L269-L277" target="_blank" rel="noopener"&gt;detects the CPU features once, for all the resolvers, and then applies these relocations&lt;/a&gt;: for each one, it &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/sysdeps/x86_64/dl-irel.h#L42-L46" target="_blank" rel="noopener"&gt;calls the resolver and writes the result&lt;/a&gt; into the &lt;code&gt;.got.plt&lt;/code&gt; slot.&lt;/p&gt;&#10;&lt;p&gt;You can see for yourself how one slot gets filled with the right address. &lt;code&gt;nm&lt;/code&gt; shows the address of the &lt;code&gt;memcmp&lt;/code&gt; resolver (the &lt;code&gt;i&lt;/code&gt; type means IFUNC), and the &lt;code&gt;IRELATIVE&lt;/code&gt; relocation with this address as its addend shows which &lt;code&gt;.got.plt&lt;/code&gt; slot gets the result:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; nm demo_static &lt;span class="p"&gt;|&lt;/span&gt; grep -w memcmp&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0000000000438ab0 i memcmp&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -rW demo_static &lt;span class="p"&gt;|&lt;/span&gt; grep 438ab0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;00000000004a4058 0000000000000025 R_X86_64_IRELATIVE 438ab0&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;And here's the same slot in pwndbg, right after &lt;code&gt;exec&lt;/code&gt; and then at &lt;code&gt;main&lt;/code&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;# right after exec, before any startup code: the memcmp slot points into its own .plt stub&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; starti&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Program stopped.&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0x0000000000401600 in _start ()&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; telescope 0x4a4058 1&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;00:0000│ 0x4a4058 (_GLOBAL_OFFSET_TABLE_&amp;#43;112) —▸ 0x401076 ◂— nop &#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;# and the stub itself is just a jmp through the slot&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; x/2i 0x401070&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x401070:&#9;jmp QWORD PTR [rip&amp;#43;0xa2fe2] # 0x4a4058&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x401076:&#9;xchg ax,ax&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;# at main, the startup code has already called the resolver&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; b main&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Breakpoint 1 at 0x401729&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; c&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Breakpoint 1, 0x0000000000401729 in main ()&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; telescope 0x4a4058 1&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;00:0000│ 0x4a4058 (_GLOBAL_OFFSET_TABLE_&amp;#43;112) —▸ 0x439c80 (__memcmp_avx2_movbe) ◂— cmp rdx, 0x20&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;Right after &lt;code&gt;exec&lt;/code&gt;, the slot points to &lt;code&gt;0x401076&lt;/code&gt;, the &lt;code&gt;nop&lt;/code&gt; that follows the &lt;code&gt;jmp&lt;/code&gt; in its own &lt;code&gt;.plt&lt;/code&gt; stub. And the stub is just this &lt;code&gt;jmp&lt;/code&gt; through the slot: no heavy stuff like &lt;code&gt;PLT0&lt;/code&gt; or lazy binding, because there's no &lt;code&gt;_dl_runtime_resolve&lt;/code&gt; to call later. By the time &lt;code&gt;main&lt;/code&gt; runs, &lt;code&gt;__libc_start_main&lt;/code&gt; has already called the resolver at &lt;code&gt;0x438ab0&lt;/code&gt; and written its answer into the slot: &lt;code&gt;__memcmp_avx2_movbe&lt;/code&gt;, the AVX2 version my CPU got. So the relocation names the resolver, and the slot ends up holding its answer.&lt;/p&gt;&#10;&lt;p&gt;That's all the PLT does in a static binary: no dynamic linking, only IFUNCs. But this doesn't mean that &lt;code&gt;IRELATIVE&lt;/code&gt; relocations only appear in static binaries: the shared &lt;code&gt;libc.so.6&lt;/code&gt; has 46 of them too, next to the usual &lt;code&gt;JUMP_SLOT&lt;/code&gt; and &lt;code&gt;GLOB_DAT&lt;/code&gt;. And one more thing for RE: these stubs have no names in &lt;code&gt;objdump&lt;/code&gt;. It shows the calls to &lt;code&gt;memcmp&lt;/code&gt; as &lt;code&gt;call 401070 &amp;lt;_init+0x70&amp;gt;&lt;/code&gt; (&lt;code&gt;.plt&lt;/code&gt; just happens to follow &lt;code&gt;.init&lt;/code&gt; in this case), not as &lt;code&gt;memcmp@plt&lt;/code&gt; like in a dynamic binary. To find out what's behind a stub, take the slot from its &lt;code&gt;jmp&lt;/code&gt; and look it up in &lt;code&gt;readelf -r&lt;/code&gt;: the addend of its &lt;code&gt;IRELATIVE&lt;/code&gt; relocation is the actual resolver.&lt;/p&gt;&#10;&lt;h3 id="-static-pie-static-but-rebased-for-aslr"&gt;&lt;code&gt;-static-pie&lt;/code&gt;: static, but rebased for ASLR&lt;/h3&gt;&#10;&lt;p&gt;You probably remember &lt;code&gt;-static-pie&lt;/code&gt; from &lt;a href="#pie-or-when-even-your-own-addresses-move"&gt;the PIE part&lt;/a&gt;: the kernel maps it at a random address, and it has 1099 &lt;code&gt;R_X86_64_RELATIVE&lt;/code&gt; relocations to rebase itself. But who actually applies them, if it's static and there's no &lt;code&gt;ld.so&lt;/code&gt; at all?&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; gcc -O0 -fno-stack-protector -fcf-protection&lt;span class="o"&gt;=&lt;/span&gt;none -static-pie -o demo_staticpie demo.c&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -l demo_staticpie &lt;span class="p"&gt;|&lt;/span&gt; grep -E &lt;span class="s1"&gt;&amp;#39;INTERP|DYNAMIC&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; DYNAMIC 0x00000000000abda8 0x00000000000acda8 0x00000000000acda8&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -d demo_staticpie &lt;span class="p"&gt;|&lt;/span&gt; grep NEEDED &lt;span class="c1"&gt;# nothing, no shared libraries&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -rW demo_staticpie &lt;span class="p"&gt;|&lt;/span&gt; grep -oE &lt;span class="s1"&gt;&amp;#39;R_X86_64_[A-Z_]+&amp;#39;&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; sort &lt;span class="p"&gt;|&lt;/span&gt; uniq -c&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 22 R_X86_64_IRELATIVE&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1099 R_X86_64_RELATIVE&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;# stop at the memcmp resolver and see who calls it&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; b memcmp_ifunc&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Breakpoint 1 at 0x3fc70&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; r&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Breakpoint 1, 0x00007ffff7f89c70 in memcmp_ifunc ()&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; bt&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;#0 0x00007ffff7f89c70 in memcmp_ifunc ()&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;#1 0x00007ffff7f6b482 in _dl_relocate_static_pie ()&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;#2 0x00007ffff7f53910 in __libc_start_main_impl ()&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;#3 0x00007ffff7f516e1 in _start ()&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;So &lt;code&gt;-static-pie&lt;/code&gt; has a &lt;code&gt;PT_DYNAMIC&lt;/code&gt; segment (that's how its relocations are found), but no &lt;code&gt;PT_INTERP&lt;/code&gt; and no &lt;code&gt;DT_NEEDED&lt;/code&gt;. Without a loader, glibc has to do it by itself, with a small piece of its code linked right into the binary: &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-reloc-static-pie.c#L36" target="_blank" rel="noopener"&gt;&lt;code&gt;_dl_relocate_static_pie&lt;/code&gt;&lt;/a&gt;. It calculates the binary's load address and &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-reloc-static-pie.c#L79" target="_blank" rel="noopener"&gt;runs the same relocation code as &lt;code&gt;ld.so&lt;/code&gt;&lt;/a&gt; over it. The 22 &lt;code&gt;IRELATIVE&lt;/code&gt; relocations are applied in the same pass, which is why &lt;code&gt;memcmp_ifunc&lt;/code&gt; (that's the resolver's own name, which &lt;code&gt;nm&lt;/code&gt; above showed as &lt;code&gt;memcmp&lt;/code&gt; with the &lt;code&gt;i&lt;/code&gt; type) is called from &lt;code&gt;_dl_relocate_static_pie&lt;/code&gt; and not from &lt;code&gt;__libc_start_main&lt;/code&gt;, like in the plain static binary. The order is still right, though: &lt;code&gt;__libc_start_main&lt;/code&gt; calls it &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/csu/libc-start.c#L269-L274" target="_blank" rel="noopener"&gt;only after the CPU features are detected&lt;/a&gt;.&lt;/p&gt;&#10;&lt;h2 id="got1-to-walk-them-all"&gt;&lt;code&gt;GOT[1]&lt;/code&gt; to walk them all&lt;/h2&gt;&#10;&lt;p&gt;Remember that magic &lt;code&gt;GOT[1]&lt;/code&gt; from the lazy binding part? It holds the &lt;code&gt;link_map&lt;/code&gt; of the executable, and &lt;code&gt;link_map&lt;/code&gt; is more than just a descriptor of one object. It's also a node in a doubly linked list of every loaded object (the executable, every shared library, the vDSO and &lt;code&gt;ld.so&lt;/code&gt; itself), and each node keeps its object's load address (&lt;code&gt;l_addr&lt;/code&gt;) and name (&lt;code&gt;l_name&lt;/code&gt;). So just one RIP-relative load from your own &lt;code&gt;.got.plt&lt;/code&gt;, no syscalls, and you have the key to the whole map of what's loaded and where, without touching &lt;code&gt;/proc/self/maps&lt;/code&gt;. Walking the loader's module list is everyday work for shellcode and malware (on Windows, it even has a special name: PEB walking). On Linux, the more robust and portable way into the same list is the &lt;code&gt;r_debug&lt;/code&gt; structure that &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/link.h#L86-L92" target="_blank" rel="noopener"&gt;&lt;code&gt;DT_DEBUG&lt;/code&gt; points to&lt;/a&gt;, and &lt;code&gt;GOT[1]&lt;/code&gt; is just a cheaper way in.&lt;/p&gt;&#10;&lt;p&gt;Shellcode has no debug info (sad), so it has to walk the list by raw offsets, and on x86-64 they're simple: &lt;code&gt;l_addr&lt;/code&gt; at &lt;code&gt;+0x00&lt;/code&gt;, &lt;code&gt;l_name&lt;/code&gt; at &lt;code&gt;+0x08&lt;/code&gt;, &lt;code&gt;l_ld&lt;/code&gt; at &lt;code&gt;+0x10&lt;/code&gt;, &lt;code&gt;l_next&lt;/code&gt; at &lt;code&gt;+0x18&lt;/code&gt; and &lt;code&gt;l_prev&lt;/code&gt; at &lt;code&gt;+0x20&lt;/code&gt;, 8 bytes each, no padding. They don't (I hope) change between glibc versions, because these five fields are the public head of &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/link.h#L101-L110" target="_blank" rel="noopener"&gt;&lt;code&gt;struct link_map&lt;/code&gt;&lt;/a&gt;, which glibc's header calls &amp;quot;part of the protocol with the debugger&amp;quot; and &amp;quot;the same format used in SVR4&amp;quot;. The internal fields after them (&lt;code&gt;l_info&lt;/code&gt;, &lt;code&gt;l_scope&lt;/code&gt; and the rest) may change between versions, but the walk doesn't need any of them anyway. The executable's node is the first one in the list, so we can start right from &lt;code&gt;GOT[1]&lt;/code&gt; and just follow &lt;code&gt;l_next&lt;/code&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;# GOT[1] is the second slot of .got.plt (0x3fe8 &amp;#43; 8), so read it and follow l_next until it&amp;#39;s NULL&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; b main&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Breakpoint 1 at 0x113d&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; r&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Breakpoint 1, 0x000055555555513d in main ()&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; set $lm = *(struct link_map **)$rebase(0x3ff0)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; while $lm&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &amp;gt; printf &amp;#34;Base: 0x%016lx, Name: %s, Next: 0x%016lx\n&amp;#34;, $lm-&amp;gt;l_addr, $lm-&amp;gt;l_name, $lm-&amp;gt;l_next&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &amp;gt; set $lm = $lm-&amp;gt;l_next&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &amp;gt;end&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Base: 0x0000555555554000, Name: , Next: 0x00007ffff7fbd000&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Base: 0x00007ffff7fc5000, Name: linux-vdso.so.1, Next: 0x00007ffff7fbd7f0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Base: 0x00007ffff7dc6000, Name: /lib/x86_64-linux-gnu/libc.so.6, Next: 0x00007ffff7ffe0a0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Base: 0x00007ffff7fc7000, Name: /lib64/ld-linux-x86-64.so.2, Next: 0x0000000000000000&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;Four nodes, four objects: the executable, the vDSO, libc and &lt;code&gt;ld.so&lt;/code&gt;. The executable has no name: the loader didn't open it (the kernel did), so it &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/rtld.c#L1596" target="_blank" rel="noopener"&gt;creates its &lt;code&gt;link_map&lt;/code&gt; with an empty one&lt;/a&gt;. &lt;code&gt;info proc mappings&lt;/code&gt; confirms the numbers: libc's first mapping starts at &lt;code&gt;0x7ffff7dc6000&lt;/code&gt;, exactly its &lt;code&gt;l_addr&lt;/code&gt;. Strictly speaking, &lt;code&gt;l_addr&lt;/code&gt; isn't the load address, but the difference between the addresses in the file and in memory. For a PIE or a shared library, whose addresses in the file start at 0, it's the same thing, but for a non-PIE executable &lt;code&gt;l_addr&lt;/code&gt; is just 0. And by the way, pwndbg has the &lt;code&gt;linkmap&lt;/code&gt; command that prints the same list, if you don't like scripting gdb.&lt;/p&gt;&#10;&lt;p&gt;There's one catch to remember, though: all this works only with lazy binding. &lt;a href="#full-relro-resolve-everything-up-front-then-freeze-it"&gt;The full RELRO part&lt;/a&gt; showed that with &lt;code&gt;-z now&lt;/code&gt; the loader doesn't fill &lt;code&gt;GOT[1]&lt;/code&gt; and &lt;code&gt;GOT[2]&lt;/code&gt; at all, because nobody will ever call the resolver. The slots are still there, at the start of &lt;code&gt;.got&lt;/code&gt;, but they hold zeros:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;# full RELRO: the reserved slots at the start of .got (0x3fb8)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; b main&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Breakpoint 1 at 0x113d&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; r&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Breakpoint 1, 0x000055555555513d in main ()&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; telescope $rebase(0x3fb8) 3&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;00:0000│ 0x555555557fb8 (_GLOBAL_OFFSET_TABLE_) ◂— 0x3dc8&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;01:0008│ 0x555555557fc0 (_GLOBAL_OFFSET_TABLE_&amp;#43;8) ◂— 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;02:0010│ 0x555555557fc8 (_GLOBAL_OFFSET_TABLE_&amp;#43;16) ◂— 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;For real malware, it's not a big deal: the technique from the next section doesn't need anyone's GOT at all, because it finds libc in memory by itself. &lt;code&gt;GOT[1]&lt;/code&gt; is just a civilized shortcut.&lt;/p&gt;&#10;&lt;h2 id="how-malware-mimics-the-loader-dynamic-import-resolution"&gt;How malware mimics the loader: dynamic import resolution&lt;/h2&gt;&#10;&lt;p&gt;A normal import table is a gift to an analyst: &lt;code&gt;nm -D&lt;/code&gt; on a sample with &lt;code&gt;connect&lt;/code&gt;, &lt;code&gt;ptrace&lt;/code&gt; and &lt;code&gt;socket&lt;/code&gt; in its &lt;code&gt;.dynsym&lt;/code&gt; tells more than enough before it even runs. That's why packed and evasive samples often don't have one, and resolve their imports at runtime by themselves, partially doing the loader's job. It usually takes three steps: find libc in memory, find the needed functions in it, and call them through the resolved addresses.&lt;/p&gt;&#10;&lt;p&gt;The first step is the most interesting one. Any pointer into libc will do, and, fortunately, &lt;code&gt;main&lt;/code&gt; always has one: its return address points into &lt;code&gt;__libc_start_call_main&lt;/code&gt;. libc's ELF header sits at the start of its first page, so round the pointer down to a page and go back one page at a time until the memory starts with the magic &lt;code&gt;\x7fELF&lt;/code&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;# main&amp;#39;s return address points into libc&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; b main&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Breakpoint 1 at 0x113d&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; r&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Breakpoint 1, 0x000055555555513d in main ()&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; telescope $rbp&amp;#43;8 1&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;00:0000│&amp;#43;008 0x7fffffffea98 —▸ 0x7ffff7defca8 (__libc_start_call_main&amp;#43;120) ◂— mov edi, eax&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;# round it down to a page and go back until the ELF magic (0x464c457f is &amp;#34;\x7fELF&amp;#34; as a little-endian int)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; set $p = *(long *)($rbp &amp;#43; 8) &amp;amp; ~0xfff&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; while *(int *)$p != 0x464c457f&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &amp;gt; set $p -= 0x1000&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &amp;gt;end&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; p/x $p&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;$1 = 0x7ffff7dc6000&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; vmmap $p&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;LEGEND: STACK | HEAP | CODE | DATA | WX | RODATA&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Start End Perm Size Offset File (set vmmap-prefer-relpaths on)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x7ffff7dc3000 0x7ffff7dc6000 rw-p 3000 0 [anon_7ffff7dc3]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;► 0x7ffff7dc6000 0x7ffff7dee000 r--p 28000 0 /usr/lib/x86_64-linux-gnu/libc.so.6 &amp;#43;0x0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x7ffff7dee000 0x7ffff7f51000 r-xp 163000 28000 /usr/lib/x86_64-linux-gnu/libc.so.6&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;The scan stops right at &lt;code&gt;0x7ffff7dc6000&lt;/code&gt;, the same address as libc's &lt;code&gt;l_addr&lt;/code&gt; in the &lt;code&gt;GOT[1]&lt;/code&gt; walk above. libc's mappings should (I believe) go back to back, so the scan never steps into an unmapped page. Once you have the ELF header, find &lt;code&gt;PT_DYNAMIC&lt;/code&gt; in the program headers, and after that, &lt;code&gt;.dynamic&lt;/code&gt; gives you &lt;code&gt;DT_GNU_HASH&lt;/code&gt;, &lt;code&gt;DT_SYMTAB&lt;/code&gt; and &lt;code&gt;DT_STRTAB&lt;/code&gt;, the same tables &lt;code&gt;gnuhash.py&lt;/code&gt; read from the file. One catch here: in memory, these pointers are usually already absolute, because the loader &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/get-dynamic-info.h#L72-L91" target="_blank" rel="noopener"&gt;adds the load address to them in place&lt;/a&gt;. For libc, &lt;code&gt;DT_GNU_HASH&lt;/code&gt; is &lt;code&gt;0x4528&lt;/code&gt; in the file and &lt;code&gt;0x7ffff7dca528&lt;/code&gt; in memory. But if &lt;code&gt;.dynamic&lt;/code&gt; is read-only, like in the vDSO, it stays as it is (&lt;code&gt;0x1e0&lt;/code&gt; there), so teach your hand-made resolver to handle both cases.&lt;/p&gt;&#10;&lt;p&gt;The second step is where the hashes come in. A &lt;code&gt;&amp;quot;ptrace&amp;quot;&lt;/code&gt; string in the sample is just as visible as a name in the import table, so samples usually store only a hash of each name and compare it with the hashes of libc's symbols. If the hash is djb2, the sample can even cheat: use libc's own &lt;code&gt;.gnu.hash&lt;/code&gt; and do what &lt;code&gt;gnuhash.py&lt;/code&gt; does. With any other hash (ROR13 from the Windows shellcoders' world, CRC32, FNV and so on), it just walks all of &lt;code&gt;.dynsym&lt;/code&gt; and hashes every name. The third step is the easiest one: the resolved addresses go into a private table, kind of the sample's own hand-made GOT, and all the calls go through it.&lt;/p&gt;&#10;&lt;p&gt;For RE, the import table is gone, but the work the sample has to do still leaves traces. A binary that magically talks to the network with nothing like &lt;code&gt;socket&lt;/code&gt; in its imports is already suspicious. In the code, try to look for a loop that compares memory with &lt;code&gt;0x464c457f&lt;/code&gt; while stepping back by &lt;code&gt;0x1000&lt;/code&gt;, and for the hash loop from &lt;a href="#gnuhash-vs-hash-finding-puts-in-libc-fast"&gt;the &lt;code&gt;.gnu.hash&lt;/code&gt; part&lt;/a&gt;. The hashes are good for hunting too: pre-calculate them for the interesting names and search the binary for these constants. For djb2, for instance, &lt;code&gt;ptrace&lt;/code&gt; is &lt;code&gt;0x159412a4&lt;/code&gt;, &lt;code&gt;socket&lt;/code&gt; is &lt;code&gt;0x1c31032e&lt;/code&gt; and &lt;code&gt;execve&lt;/code&gt; is &lt;code&gt;0xfc2c9fc5&lt;/code&gt;.&lt;/p&gt;&#10;&lt;p&gt;None of this is exotic once you know how the loader works: the malware is just trying to be its own &lt;code&gt;_dl_fixup&lt;/code&gt;.&lt;/p&gt;&#10;&lt;h2 id="trust-but-verify-checksec"&gt;Trust, but verify: &lt;code&gt;checksec&lt;/code&gt;&lt;/h2&gt;&#10;&lt;p&gt;And last but not the best: &lt;code&gt;checksec&lt;/code&gt;, the usual first look at an unknown binary in tons of writeups. Debian 13 still ships version 2.6.0 from 2022, so I installed &lt;a href="https://github.com/slimm609/checksec/releases/tag/3.2.0" target="_blank" rel="noopener"&gt;the current 3.2.0&lt;/a&gt; from GitHub. Its &lt;code&gt;RELRO&lt;/code&gt; verdict comes from the same two things we've been checking by hand on &lt;code&gt;demo_partial&lt;/code&gt; and &lt;code&gt;demo_full&lt;/code&gt;, the &lt;code&gt;GNU_RELRO&lt;/code&gt; segment and the &lt;code&gt;BIND_NOW&lt;/code&gt; flag. By default, it prints a school-level ASCII art banner and a table more than 300 characters wide, so I use its JSON output and &lt;code&gt;jq&lt;/code&gt; to keep only the &lt;code&gt;RELRO&lt;/code&gt; field I need:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; checksec file -o json ./demo_partial &lt;span class="p"&gt;|&lt;/span&gt; jq -r &lt;span class="s1"&gt;&amp;#39;.[0].checks.relro.value&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Partial RELRO&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; checksec file -o json ./demo_full &lt;span class="p"&gt;|&lt;/span&gt; jq -r &lt;span class="s1"&gt;&amp;#39;.[0].checks.relro.value&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Full RELRO&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;&lt;a href="https://github.com/slimm609/checksec/blob/3.2.0/pkg/checksec/relro.go#L31-L45" target="_blank" rel="noopener"&gt;The check itself&lt;/a&gt; is just a few lines of Go (yes, it was rewritten in Go). Specifically, &lt;code&gt;BIND_NOW&lt;/code&gt; in any of its three forms (the &lt;code&gt;DT_BIND_NOW&lt;/code&gt; tag, or the &lt;code&gt;NOW&lt;/code&gt; flag in &lt;code&gt;DT_FLAGS&lt;/code&gt; or &lt;code&gt;DT_FLAGS_1&lt;/code&gt;) means Full, otherwise &lt;code&gt;GNU_RELRO&lt;/code&gt; means Partial, and with neither of them, it's just No. But look at the order: &lt;code&gt;BIND_NOW&lt;/code&gt; comes first, and checksec doesn't even look for &lt;code&gt;GNU_RELRO&lt;/code&gt; if it's set. So a binary linked with &lt;code&gt;-z norelro -z now&lt;/code&gt; suddenly gets &amp;quot;Full RELRO&amp;quot;, without any RELRO at all:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; gcc -O0 -fno-stack-protector -fcf-protection&lt;span class="o"&gt;=&lt;/span&gt;none -o demo_nowonly demo.c -Wl,-z,norelro,-z,now&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -l demo_nowonly &lt;span class="p"&gt;|&lt;/span&gt; grep GNU_RELRO &lt;span class="c1"&gt;# nothing&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -d demo_nowonly &lt;span class="p"&gt;|&lt;/span&gt; grep -Ei &lt;span class="s1"&gt;&amp;#39;bind|flags&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x000000000000001e (FLAGS) BIND_NOW&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x000000006ffffffb (FLAGS_1) Flags: NOW PIE&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; checksec file -o json ./demo_nowonly &lt;span class="p"&gt;|&lt;/span&gt; jq -r &lt;span class="s1"&gt;&amp;#39;.[0].checks.relro.value&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Full RELRO&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;# at main: resolved like in full RELRO, but is it read-only?&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; b main&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Breakpoint 1 at 0x113d&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; r&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Breakpoint 1, 0x000055555555513d in main ()&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; got -r&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;State of the GOT of /work/demo_nowonly:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;GOT protection: No RELRO | Found 6 GOT entries passing the filter&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;[0x555555557330] puts@GLIBC_2.2.5 -&amp;gt; 0x7ffff7e46300 (puts) ◂— push r14&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;[0x555555557338] __libc_start_main@GLIBC_2.34 -&amp;gt; 0x7ffff7defce0 (__libc_start_main) ◂— push r15&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;[0x555555557340] _ITM_deregisterTMCloneTable -&amp;gt; 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;[0x555555557348] __gmon_start__ -&amp;gt; 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;[0x555555557350] _ITM_registerTMCloneTable -&amp;gt; 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;[0x555555557358] __cxa_finalize@GLIBC_2.2.5 -&amp;gt; 0x7ffff7e07b90 (__cxa_finalize) ◂— push r15&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; vmmap 0x555555557330&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;LEGEND: STACK | HEAP | CODE | DATA | WX | RODATA&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Start End Perm Size Offset File (set vmmap-prefer-relpaths on)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x555555556000 0x555555557000 r--p 1000 2000 demo_nowonly&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;► 0x555555557000 0x555555558000 rw-p 1000 2000 demo_nowonly &amp;#43;0x330&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x7ffff7dc3000 0x7ffff7dc6000 rw-p 3000 0 [anon_7ffff7dc3]&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;At &lt;code&gt;main&lt;/code&gt;, everything is resolved up front, just like with full RELRO, but nobody made the GOT read-only afterwards. The &lt;code&gt;puts&lt;/code&gt; slot sits on an &lt;code&gt;rw-p&lt;/code&gt; page, and pwndbg correctly says &amp;quot;No RELRO&amp;quot;. Funny enough, Debian's old 2.6.0 gets it right, because it &lt;a href="https://github.com/slimm609/checksec/blob/2.6.0/checksec#L702-L711" target="_blank" rel="noopener"&gt;checks &lt;code&gt;GNU_RELRO&lt;/code&gt; first&lt;/a&gt;. So don't trust the fancy tools blindly.&lt;/p&gt;&#10;&lt;p&gt;Happy hacking!&lt;/p&gt;&#10;</description></item><item><title>Multiple ways to find the auxiliary vector (auxv)</title><link>https://pugachev.io/2026/10/25/multiple-ways-to-find-the-auxiliary-vector-auxv/</link><pubDate>Sun, 25 Oct 2026 17:46:59 +0100</pubDate><guid>https://pugachev.io/2026/10/25/multiple-ways-to-find-the-auxiliary-vector-auxv/</guid><description>&lt;p&gt;I've heard the term &amp;quot;auxiliary vector&amp;quot; a few times before, but never had time to dig into it. So I decided to finally make myself a bit smarter and learn what the &lt;code&gt;auxv&lt;/code&gt; is. To make it even more satisfying, I set a goal to find as many ways to get to the &lt;code&gt;auxv&lt;/code&gt; as I could, from a one-liner in the shell down to the &amp;quot;no libc, no envp, scan the stack and pray&amp;quot; method.&lt;/p&gt;&#10;&lt;p&gt;Everything here is x86-64 Linux with the System V AMD64 ABI.&lt;/p&gt;&#10;&lt;h2 id="what-the-auxv-actually-is"&gt;What the &lt;code&gt;auxv&lt;/code&gt; actually is&lt;/h2&gt;&#10;&lt;p&gt;The man page for &lt;code&gt;getauxval(3)&lt;/code&gt; calls it&lt;/p&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;a mechanism that the kernel's ELF binary loader uses to pass certain information to user space when a program is executed.&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;p&gt;That's nice, but it doesn't really say &lt;em&gt;why&lt;/em&gt; it exists. When the kernel's ELF loader (&lt;code&gt;fs/binfmt_elf.c&lt;/code&gt;) passes control to a new process, the dynamic linker hasn't run yet: there is no libc, no TLS, no resolved symbols, nothing. But &lt;code&gt;ld.so&lt;/code&gt; still needs to bootstrap itself somehow, and for that it needs things only the kernel could know. Where are the program headers mapped? Where is the vDSO? What's the page size? Is this process privileged? The C runtime can't ask anyone, so the &lt;code&gt;auxv&lt;/code&gt; is how the kernel passes this knowledge. It's a small array of key/value pairs pushed onto the initial stack, right next to &lt;code&gt;argv&lt;/code&gt; and &lt;code&gt;envp&lt;/code&gt;.&lt;/p&gt;&#10;&lt;p&gt;Each entry is an &lt;a href="https://github.com/gnutools/glibc/blob/release/2.44/master/elf/elf.h#L1187-L1197" target="_blank" rel="noopener"&gt;&lt;code&gt;Elf64_auxv_t&lt;/code&gt;&lt;/a&gt; struct:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="kt"&gt;uint64_t&lt;/span&gt; &lt;span class="n"&gt;a_type&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="cm"&gt;/* AT_* tag */&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;union&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="kt"&gt;uint64_t&lt;/span&gt; &lt;span class="n"&gt;a_val&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="cm"&gt;/* integer value, or a pointer */&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;a_un&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;Elf64_auxv_t&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Here are the most interesting ones with their tag numbers, since later we'll need to match the raw numbers:&lt;/p&gt;&#10;&lt;table&gt;&#10;&#9;&lt;thead&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;Tag&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;#&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;Meaning&lt;/th&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&lt;/thead&gt;&#10;&#9;&lt;tbody&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;AT_PHDR&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;3&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;address of the program's ELF program headers&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;AT_PHENT&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;4&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;size of one program header (always &lt;code&gt;0x38&lt;/code&gt; = &lt;code&gt;sizeof(Elf64_Phdr)&lt;/code&gt;)&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;AT_PHNUM&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;5&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;number of program headers&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;AT_PAGESZ&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;6&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;page size (&lt;code&gt;0x1000&lt;/code&gt; on x86-64)&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;AT_BASE&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;7&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;base address of the interpreter (&lt;code&gt;ld.so&lt;/code&gt;)&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;AT_ENTRY&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;9&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;entry point of the program&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;AT_UID&lt;/code&gt; / &lt;code&gt;AT_EUID&lt;/code&gt; / &lt;code&gt;AT_GID&lt;/code&gt; / &lt;code&gt;AT_EGID&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;11-14&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;the four credential IDs&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;AT_HWCAP&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;16&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;CPU capability bitmask&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;AT_CLKTCK&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;17&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;times()&lt;/code&gt; tick frequency (&lt;code&gt;100&lt;/code&gt; on x86-64)&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;AT_SECURE&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;23&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;secure execution mode (setuid and similar)&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;AT_RANDOM&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;25&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;pointer to kernel-generated random bytes&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;AT_EXECFN&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;31&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;pointer to the pathname used to exec the binary&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;AT_SYSINFO_EHDR&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;33&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;base of the vDSO's ELF header&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;AT_NULL&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;0&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;terminating &lt;code&gt;{0, 0}&lt;/code&gt; entry&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&lt;/tbody&gt;&#10;&lt;/table&gt;&#10;&lt;p&gt;The full list is in the kernel's &lt;a href="https://github.com/torvalds/linux/blob/v6.18/include/uapi/linux/auxvec.h" target="_blank" rel="noopener"&gt;&lt;code&gt;include/uapi/linux/auxvec.h&lt;/code&gt;&lt;/a&gt;, and the x86-specific additions are in &lt;a href="https://github.com/torvalds/linux/blob/v6.18/arch/x86/include/uapi/asm/auxvec.h" target="_blank" rel="noopener"&gt;&lt;code&gt;arch/x86/include/uapi/asm/auxvec.h&lt;/code&gt;&lt;/a&gt; (x86 adds &lt;code&gt;AT_SYSINFO_EHDR&lt;/code&gt; and, for 32-bit, &lt;code&gt;AT_SYSINFO&lt;/code&gt;).&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;AT_RANDOM&lt;/code&gt; is the most thrilling one: glibc takes those 16 bytes to generate the stack canary and the pointer guard, so it shows up in exploitation writeups all the time.&lt;/p&gt;&#10;&lt;p&gt;The kernel builds the &lt;code&gt;auxv&lt;/code&gt; in the &lt;code&gt;create_elf_tables()&lt;/code&gt; function inside &lt;a href="https://github.com/torvalds/linux/blob/v6.18/fs/binfmt_elf.c#L164" target="_blank" rel="noopener"&gt;&lt;code&gt;fs/binfmt_elf.c&lt;/code&gt;&lt;/a&gt;, if you want to see how it's done for some reason. The order is defined there (and it hasn't changed in years): &lt;a href="https://github.com/torvalds/linux/blob/v6.18/fs/binfmt_elf.c#L247" target="_blank" rel="noopener"&gt;&lt;code&gt;ARCH_DLINFO&lt;/code&gt; first&lt;/a&gt; (on x86-64, that's &lt;code&gt;AT_SYSINFO_EHDR&lt;/code&gt; for the vDSO plus &lt;code&gt;AT_MINSIGSTKSZ&lt;/code&gt;), then &lt;code&gt;AT_HWCAP&lt;/code&gt;, &lt;code&gt;AT_PAGESZ&lt;/code&gt;, &lt;code&gt;AT_CLKTCK&lt;/code&gt;, &lt;code&gt;AT_PHDR&lt;/code&gt; and so on.&lt;/p&gt;&#10;&lt;h2 id="the-layout-at-the-entry-point"&gt;The layout at the entry point&lt;/h2&gt;&#10;&lt;p&gt;I heard that examples and pictures make a text easier to understand, so here is one in ASCII. When the kernel passes control to &lt;code&gt;_start&lt;/code&gt;, the &lt;code&gt;rsp&lt;/code&gt; register points to &lt;code&gt;argc&lt;/code&gt;, while &lt;code&gt;argv&lt;/code&gt;, &lt;code&gt;envp&lt;/code&gt; and the &lt;code&gt;auxv&lt;/code&gt; are above it, toward higher addresses:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Higher addresses&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; ...&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Elf64_auxv_t { AT_NULL, 0 } &amp;lt;- terminating entry&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; ...&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Elf64_auxv_t { AT_SYSINFO_EHDR, ... } &amp;lt;- auxv[0]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; NULL &amp;lt;- envp terminator&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; envp[n-1]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; ...&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; envp[0]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; NULL &amp;lt;- argv terminator&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; argv[argc-1]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; ...&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; argv[0]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; argc &amp;lt;- RSP points here at _start&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Lower addresses&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;So the &lt;code&gt;auxv&lt;/code&gt; starts right after the &lt;code&gt;NULL&lt;/code&gt; pointer that terminates &lt;code&gt;envp&lt;/code&gt;, and &lt;code&gt;envp&lt;/code&gt; starts right after the &lt;code&gt;NULL&lt;/code&gt; that terminates &lt;code&gt;argv&lt;/code&gt;. This highlights two things that most of the methods below rely on:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;If you have &lt;code&gt;envp&lt;/code&gt;, finding the &lt;code&gt;auxv&lt;/code&gt; is easy: walk to the &lt;code&gt;NULL&lt;/code&gt; and get the next entry.&lt;/li&gt;&#10;&lt;li&gt;The whole structure above exists only on the initial stack of the main thread. Another thread created with &lt;code&gt;clone(2)&lt;/code&gt; gets a new stack without any of this stuff, and, unfortunately, as we'll see later, that's where the stack-scanning method stops working.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;Now, the methods, roughly from normal to 1337.&lt;/p&gt;&#10;&lt;h2 id="method-1-ld_show_auxv1-the-shell-one-liner"&gt;Method 1: &lt;code&gt;LD_SHOW_AUXV=1&lt;/code&gt; (the shell one-liner)&lt;/h2&gt;&#10;&lt;p&gt;The easy one: glibc's dynamic linker will dump the whole vector for you if you just set one environment variable:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; &lt;span class="nv"&gt;LD_SHOW_AUXV&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt; /bin/true&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_SYSINFO_EHDR: 0x7f67fbff3000&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_MINSIGSTKSZ: 1776&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_HWCAP: f8bfbff&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_PAGESZ: 4096&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_CLKTCK: 100&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_PHDR: 0x555645065040&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_PHENT: 56&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_PHNUM: 14&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_BASE: 0x7f67fbff5000&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_FLAGS: 0x0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_ENTRY: 0x5556450677e0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_UID: 1000&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_EUID: 1000&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_GID: 1000&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_EGID: 1000&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_SECURE: 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_RANDOM: 0x7ffe1906fab9&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_HWCAP2: 0x2&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_EXECFN: /bin/true&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_PLATFORM: x86_64&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_RSEQ_FEATURE_SIZE: 28&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_RSEQ_ALIGN: 32&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;This is done inside &lt;code&gt;ld.so&lt;/code&gt;: &lt;a href="https://github.com/gnutools/glibc/blob/release/2.44/master/elf/rtld.c#L2668-L2669" target="_blank" rel="noopener"&gt;&lt;code&gt;elf/rtld.c&lt;/code&gt;&lt;/a&gt; parses the environment variable and calls &lt;code&gt;_dl_show_auxv()&lt;/code&gt; from &lt;a href="https://github.com/gnutools/glibc/blob/release/2.44/master/sysdeps/unix/sysv/linux/dl-sysdep.c#L151" target="_blank" rel="noopener"&gt;&lt;code&gt;sysdeps/unix/sysv/linux/dl-sysdep.c&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;&#10;&lt;p&gt;People usually just say it &amp;quot;doesn't work for setuid/setgid and statically linked binaries&amp;quot;. Both are true, but for two completely different reasons:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;static binaries: there is no &lt;code&gt;ld.so&lt;/code&gt; in the process at all, so nobody can parse &lt;code&gt;LD_SHOW_AUXV&lt;/code&gt;, and the binary prints nothing&lt;/li&gt;&#10;&lt;li&gt;&amp;quot;secure-exec&amp;quot; (e.g. setuid) binaries: &lt;code&gt;ld.so&lt;/code&gt; &lt;em&gt;is&lt;/em&gt; there, but it totally ignores &lt;code&gt;LD_SHOW_AUXV&lt;/code&gt;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;But it's not really about the setuid bit. The guilty one is the kernel's &lt;code&gt;AT_SECURE&lt;/code&gt; flag, which is set whenever an &lt;code&gt;execve&lt;/code&gt; &lt;em&gt;gains privileges relative to its caller&lt;/em&gt;: the capability LSM (one of the Linux Security Modules, like SELinux or AppArmor) &lt;a href="https://github.com/torvalds/linux/blob/v6.18/security/commoncap.c#L989-L995" target="_blank" rel="noopener"&gt;sets it&lt;/a&gt; when the setuid/setgid bit actually changes the euid/egid values. Another case is binaries with file capabilities (set with &lt;code&gt;setcap&lt;/code&gt;). If you need more, &lt;a href="https://github.com/torvalds/linux/blob/v6.18/security/selinux/hooks.c#L2415-L2421" target="_blank" rel="noopener"&gt;SELinux sets it too&lt;/a&gt; on domain transitions (unless the policy grants the &lt;code&gt;process:noatsecure&lt;/code&gt; permission), and other LSMs can add their own cases, because why not.&lt;/p&gt;&#10;&lt;p&gt;&lt;a href="https://github.com/gnutools/glibc/blob/release/2.44/master/sysdeps/unix/sysv/linux/dl-parse_auxv.h#L48" target="_blank" rel="noopener"&gt;glibc copies&lt;/a&gt; the &lt;code&gt;AT_SECURE&lt;/code&gt; flag value into the &lt;code&gt;__libc_enable_secure&lt;/code&gt; variable, and if it's 1, &lt;code&gt;ld.so&lt;/code&gt; calls &lt;code&gt;process_envvars_secure()&lt;/code&gt; instead of the normal path, which &lt;a href="https://github.com/gnutools/glibc/blob/release/2.44/master/elf/rtld.c#L2562-L2575" target="_blank" rel="noopener"&gt;removes a whole list of &amp;quot;very dangerous&amp;quot; variables with &lt;code&gt;unsetenv()&lt;/code&gt;&lt;/a&gt;, including &lt;code&gt;LD_SHOW_AUXV&lt;/code&gt; (the list is in &lt;a href="https://github.com/gnutools/glibc/blob/release/2.44/master/sysdeps/generic/unsecvars.h#L4" target="_blank" rel="noopener"&gt;&lt;code&gt;sysdeps/generic/unsecvars.h&lt;/code&gt;&lt;/a&gt;). So the variable is gone before it can do anything.&lt;/p&gt;&#10;&lt;p&gt;The important part from above is &amp;quot;relative to its caller&amp;quot;: a setuid binary is not automatically secure (huh?). Run a setuid-root program as a normal user, and euid goes from &lt;code&gt;1000&lt;/code&gt; to &lt;code&gt;0&lt;/code&gt;, so &lt;code&gt;AT_SECURE=1&lt;/code&gt; and &lt;code&gt;LD_SHOW_AUXV&lt;/code&gt; is removed. Run the same program as root, and nothing changes: euid stays &lt;code&gt;0&lt;/code&gt;, the same as your real uid, so &lt;code&gt;AT_SECURE=0&lt;/code&gt; and the vector is dumped just fine.&lt;/p&gt;&#10;&lt;p&gt;This glibc trick with rewriting &lt;code&gt;environ&lt;/code&gt; for a secure exec will come back later and &lt;a href="#method-6-from-envp-or-environ-in-c"&gt;break one of the methods&lt;/a&gt;.&lt;/p&gt;&#10;&lt;p&gt;And just to mention, the method only works with glibc. musl has no &lt;code&gt;LD_SHOW_AUXV&lt;/code&gt; (&lt;a href="https://git.musl-libc.org/cgit/musl/tree/ldso/dynlink.c?h=v1.2.6#n1821" target="_blank" rel="noopener"&gt;its dynamic linker is quite conservative and only knows &lt;code&gt;LD_LIBRARY_PATH&lt;/code&gt; and &lt;code&gt;LD_PRELOAD&lt;/code&gt;&lt;/a&gt;), so there you have to choose one of the methods below.&lt;/p&gt;&#10;&lt;h2 id="method-2-read-procselfauxv-directly"&gt;Method 2: read &lt;code&gt;/proc/self/auxv&lt;/code&gt; directly&lt;/h2&gt;&#10;&lt;p&gt;The kernel also exposes the raw &lt;code&gt;auxv&lt;/code&gt; as a file. It's a flat array of &lt;code&gt;Elf64_auxv_t&lt;/code&gt; structs, i.e. pairs of 8-byte little-endian words, terminated by the &lt;code&gt;{0, 0}&lt;/code&gt; entry. A bit of &lt;code&gt;hexdump&lt;/code&gt; magic does the printing:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; hexdump -v -e &lt;span class="s1"&gt;&amp;#39;1/8 &amp;#34;0x%016x: &amp;#34; 1/8 &amp;#34;0x%016x\n&amp;#34;&amp;#39;&lt;/span&gt; /proc/self/auxv&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0x0000000000000021: 0x00007fa97d6f7000&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0x0000000000000033: 0x00000000000006f0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0x0000000000000010: 0x000000000f8bfbff&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0x0000000000000006: 0x0000000000001000&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0x0000000000000011: 0x0000000000000064&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;...&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0x0000000000000000: 0x0000000000000000&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;The left column is &lt;code&gt;a_type&lt;/code&gt;, and the right one is &lt;code&gt;a_val&lt;/code&gt;, so the tags are in hex here: &lt;code&gt;0x21&lt;/code&gt; is &lt;code&gt;AT_SYSINFO_EHDR&lt;/code&gt; (33), &lt;code&gt;0x33&lt;/code&gt; is &lt;code&gt;AT_MINSIGSTKSZ&lt;/code&gt; (51), &lt;code&gt;0x10&lt;/code&gt; is &lt;code&gt;AT_HWCAP&lt;/code&gt;, &lt;code&gt;0x06&lt;/code&gt; is &lt;code&gt;AT_PAGESZ&lt;/code&gt; and &lt;code&gt;0x11&lt;/code&gt; is &lt;code&gt;AT_CLKTCK&lt;/code&gt;, the same order as in the &lt;code&gt;LD_SHOW_AUXV&lt;/code&gt; output above. The last &lt;code&gt;{0, 0}&lt;/code&gt; entry is &lt;code&gt;AT_NULL&lt;/code&gt;. If you don't have &lt;code&gt;hexdump&lt;/code&gt;, &lt;code&gt;od&lt;/code&gt; can do the same:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; od -A n -t x8 -w16 -v /proc/self/auxv&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;A couple of things to know about this file. It's backed by &lt;a href="https://github.com/torvalds/linux/blob/v6.18/include/linux/mm_types.h#L1115" target="_blank" rel="noopener"&gt;&lt;code&gt;mm-&amp;gt;saved_auxv&lt;/code&gt;&lt;/a&gt;: the kernel keeps its own copy of the &lt;code&gt;auxv&lt;/code&gt; in &lt;code&gt;mm_struct&lt;/code&gt;, and &lt;code&gt;auxv_read&lt;/code&gt; in &lt;a href="https://github.com/torvalds/linux/blob/v6.18/fs/proc/base.c#L1081-L1094" target="_blank" rel="noopener"&gt;&lt;code&gt;fs/proc/base.c&lt;/code&gt;&lt;/a&gt; just returns that copy. So it's a snapshot the kernel took at &lt;code&gt;execve&lt;/code&gt;, not the &lt;code&gt;auxv&lt;/code&gt; the process is actually using. If something in the process changes the &lt;code&gt;auxv&lt;/code&gt; on the stack for some reason (and &lt;code&gt;ld.so&lt;/code&gt; actually can do that silently, &lt;a href="#when-the-api-can-lie"&gt;more on it below&lt;/a&gt;), &lt;code&gt;/proc/self/auxv&lt;/code&gt; still shows the original kernel values. In other words, the two can be different, and it's neither a feature nor a bug.&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;/proc/&amp;lt;pid&amp;gt;/auxv&lt;/code&gt; of &lt;em&gt;another&lt;/em&gt; process &lt;a href="https://github.com/torvalds/linux/blob/v6.18/fs/proc/base.c#L3674" target="_blank" rel="noopener"&gt;has mode &lt;code&gt;S_IRUSR&lt;/code&gt;&lt;/a&gt; and is &lt;a href="https://github.com/torvalds/linux/blob/v6.18/fs/proc/base.c#L1076-L1079" target="_blank" rel="noopener"&gt;protected by &lt;code&gt;PTRACE_MODE_READ_FSCREDS&lt;/code&gt;&lt;/a&gt;. That's the same &amp;quot;could I &lt;code&gt;ptrace&lt;/code&gt; you?&amp;quot; check, but in the &lt;code&gt;FSCREDS&lt;/code&gt; variant, because you access it through a filesystem &lt;code&gt;open()&lt;/code&gt; call. So it compares your fsuid (the identity that file operations use) with the target, not your real uid. In practice, you can read it for your own processes, but for someone else's you need ptrace-level access (same UID, or &lt;code&gt;CAP_SYS_PTRACE&lt;/code&gt;). And &lt;code&gt;/proc/self/auxv&lt;/code&gt; is, of course, always readable by the process itself, which makes this method so reliable: no libc, no special privileges, just open it and read it.&lt;/p&gt;&#10;&lt;h2 id="method-3-getauxval3"&gt;Method 3: &lt;code&gt;getauxval(3)&lt;/code&gt;&lt;/h2&gt;&#10;&lt;p&gt;If you're already linked against glibc (&lt;a href="https://github.com/gnutools/glibc/blob/release/2.44/master/misc/Versions#L150" target="_blank" rel="noopener"&gt;2.16+&lt;/a&gt;) or musl (1.1.0+), this is the official API:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;sys/auxv.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;pagesz&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getauxval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;AT_PAGESZ&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="cm"&gt;/* 4096 */&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;getauxval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;AT_RANDOM&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="cm"&gt;/* the 16 random bytes */&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If the tag isn't there, it returns &lt;code&gt;0&lt;/code&gt; and sets &lt;code&gt;errno&lt;/code&gt; to &lt;code&gt;ENOENT&lt;/code&gt;. But &lt;code&gt;0&lt;/code&gt; can also be a real value (e.g. &lt;code&gt;AT_SECURE&lt;/code&gt;, or &lt;code&gt;AT_UID&lt;/code&gt; for root), so clear your &lt;code&gt;errno&lt;/code&gt; before the call and check it after.&lt;/p&gt;&#10;&lt;p&gt;The funny thing I noticed while testing this method is that &lt;code&gt;getauxval&lt;/code&gt; doesn't always return what the kernel put in the &lt;code&gt;auxv&lt;/code&gt;. For &lt;code&gt;AT_HWCAP&lt;/code&gt; and &lt;code&gt;AT_HWCAP2&lt;/code&gt;, it takes a mysterious shortcut: instead of scanning the &lt;code&gt;auxv&lt;/code&gt;, it returns glibc's own internal &lt;code&gt;GLRO(dl_hwcap)&lt;/code&gt; and &lt;code&gt;GLRO(dl_hwcap2)&lt;/code&gt; (see &lt;a href="https://github.com/gnutools/glibc/blob/release/2.44/master/misc/getauxval.c#L30-L39" target="_blank" rel="noopener"&gt;&lt;code&gt;misc/getauxval.c&lt;/code&gt;&lt;/a&gt;).&lt;/p&gt;&#10;&lt;p&gt;Initially, these internal fields are just copies of the kernel's values: early in startup, &lt;a href="https://github.com/gnutools/glibc/blob/release/2.44/master/sysdeps/unix/sysv/linux/dl-parse_auxv.h#L50-L53" target="_blank" rel="noopener"&gt;&lt;code&gt;_dl_parse_auxv&lt;/code&gt; fills all &lt;code&gt;dl_hwcap*&lt;/code&gt; fields&lt;/a&gt; directly from the &lt;code&gt;auxv&lt;/code&gt; (&lt;code&gt;GLRO(dl_hwcap) = auxv_values[AT_HWCAP]&lt;/code&gt;, &lt;code&gt;GLRO(dl_hwcap2) = auxv_values[AT_HWCAP2]&lt;/code&gt; and so on). The difference comes later, and only in one of the fields: on x86, &lt;a href="https://github.com/gnutools/glibc/blob/release/2.44/master/sysdeps/x86/cpu-features.c#L1242" target="_blank" rel="noopener"&gt;&lt;code&gt;cpu-features.c&lt;/code&gt; &lt;em&gt;overwrites&lt;/em&gt; &lt;code&gt;dl_hwcap&lt;/code&gt;&lt;/a&gt; with its own custom bitmask (defined in &lt;a href="https://github.com/gnutools/glibc/blob/release/2.44/master/sysdeps/x86/dl-hwcap.h#L23-L25" target="_blank" rel="noopener"&gt;&lt;code&gt;sysdeps/x86/dl-hwcap.h&lt;/code&gt;&lt;/a&gt;: bit 0 = SSE2, bit 1 = &amp;quot;x86-64&amp;quot;, bit 2 = AVX512) instead of the kernel's proper CPUID feature bits. &lt;code&gt;dl_hwcap2&lt;/code&gt; is never touched though, so it still has the kernel value. This is what it looks like on my Debian:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;# raw kernel value from /proc/self/auxv:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_HWCAP = 0x000000000f8bfbff&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;# what getauxval hands you on the same machine:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;getauxval(AT_HWCAP) = 0x0000000000000002&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;So on x86-64, if you need the kernel's real &lt;code&gt;AT_HWCAP&lt;/code&gt; bitmask, &lt;code&gt;getauxval(AT_HWCAP)&lt;/code&gt; is a bad idea: use &lt;code&gt;/proc/self/auxv&lt;/code&gt; or find the &lt;code&gt;auxv&lt;/code&gt; on the stack yourself. For every other tag, the values are correct. And it's only a glibc thing: musl's &lt;code&gt;getauxval&lt;/code&gt; &lt;a href="https://git.musl-libc.org/cgit/musl/tree/src/misc/getauxval.c?h=v1.2.6#n8" target="_blank" rel="noopener"&gt;treats only &lt;code&gt;AT_SECURE&lt;/code&gt; specially&lt;/a&gt; and scans the &lt;code&gt;auxv&lt;/code&gt; for everything else, so there &lt;code&gt;getauxval(AT_HWCAP)&lt;/code&gt; returns the correct kernel bitmask, the same value &lt;code&gt;/proc/self/auxv&lt;/code&gt; shows.&lt;/p&gt;&#10;&lt;h2 id="method-4-prctlpr_get_auxv"&gt;Method 4: &lt;code&gt;prctl(PR_GET_AUXV)&lt;/code&gt;&lt;/h2&gt;&#10;&lt;p&gt;The bleeding edge of the kernel API (well, as of 2023): since Linux 6.4, you can ask the kernel for the &lt;code&gt;auxv&lt;/code&gt; directly, without &lt;code&gt;/proc&lt;/code&gt; (useful in a seccomp sandbox that blocks &lt;code&gt;openat&lt;/code&gt; calls but gently allows &lt;code&gt;prctl&lt;/code&gt;):&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;linux/prctl.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;sys/prctl.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;elf.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;Elf64_auxv_t&lt;/span&gt; &lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;full&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;prctl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;PR_GET_AUXV&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt; &lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;It copies &lt;code&gt;min(sizeof(mm-&amp;gt;saved_auxv), your_len)&lt;/code&gt; bytes into your buffer and returns &lt;code&gt;sizeof(mm-&amp;gt;saved_auxv)&lt;/code&gt;, the size of the kernel's whole saved array, so you can see if your buffer was too small for this. On my box it returned &lt;code&gt;416&lt;/code&gt;, i.e. &lt;code&gt;26 * sizeof(Elf64_auxv_t)&lt;/code&gt;, because &lt;code&gt;saved_auxv&lt;/code&gt; is an array of &lt;a href="https://github.com/torvalds/linux/blob/v6.18/include/linux/mm_types.h#L30" target="_blank" rel="noopener"&gt;&lt;code&gt;AT_VECTOR_SIZE&lt;/code&gt;&lt;/a&gt; longs (52 on x86-64). That's a fixed size, not the number of entries actually used. And just like &lt;code&gt;/proc/self/auxv&lt;/code&gt;, it reads the kernel's &lt;code&gt;mm-&amp;gt;saved_auxv&lt;/code&gt;, so it gives you the same initial &lt;code&gt;execve&lt;/code&gt;-time snapshot. The implementation is &lt;code&gt;prctl_get_auxv()&lt;/code&gt; in &lt;a href="https://github.com/torvalds/linux/blob/v6.18/kernel/sys.c#L2463" target="_blank" rel="noopener"&gt;&lt;code&gt;kernel/sys.c&lt;/code&gt;&lt;/a&gt;, and it's quite compact.&lt;/p&gt;&#10;&lt;p&gt;And while we're here: there is also the flip side, if you need it, namely &lt;code&gt;prctl(PR_SET_MM, PR_SET_MM_AUXV, ...)&lt;/code&gt;. It lets a process with the &lt;code&gt;CAP_SYS_RESOURCE&lt;/code&gt; capability &lt;em&gt;overwrite&lt;/em&gt; the kernel's &lt;code&gt;mm-&amp;gt;saved_auxv&lt;/code&gt;. But only this saved copy, and the &lt;a href="https://github.com/torvalds/linux/blob/v6.18/kernel/sys.c#L2171-L2176" target="_blank" rel="noopener"&gt;kernel comment&lt;/a&gt; says it directly: &amp;quot;doesn't move the auxiliary vector itself since it's pinned to &lt;code&gt;mm_struct&lt;/code&gt;&amp;quot;. So it changes what &lt;code&gt;/proc&lt;/code&gt; and &lt;code&gt;PR_GET_AUXV&lt;/code&gt; report, but the array on the stack and glibc's cached &lt;code&gt;dl_auxv&lt;/code&gt; pointer stay the same. In other words, it's a nasty way to make &lt;code&gt;/proc/&amp;lt;pid&amp;gt;/auxv&lt;/code&gt; lie, which is a fun detail for anyone doing computer forensics and trusting results blindly.&lt;/p&gt;&#10;&lt;h2 id="method-5-from-the-entry-point-in-asm"&gt;Method 5: from the entry point, in asm&lt;/h2&gt;&#10;&lt;p&gt;Now the 1337 part. Beware, this is the shellcoders' case: you're running at (or near) &lt;code&gt;_start&lt;/code&gt;, &lt;code&gt;rsp&lt;/code&gt; points to &lt;code&gt;argc&lt;/code&gt;, and you want the &lt;code&gt;auxv&lt;/code&gt; without any imports. Remember my ASCII picture from the beginning of the post: walk past &lt;code&gt;argc&lt;/code&gt;, past &lt;code&gt;argv&lt;/code&gt; and its &lt;code&gt;NULL&lt;/code&gt;, then scan &lt;code&gt;envp&lt;/code&gt; to its &lt;code&gt;NULL&lt;/code&gt;, and the &lt;code&gt;auxv&lt;/code&gt; starts right after it.&lt;/p&gt;&#10;&lt;p&gt;It's tempting to end this code with a &lt;code&gt;ret&lt;/code&gt; (as I did while testing), but then it suddenly segfaults. In proper entry-point shellcode, nobody actually called you, so there is no return address on the stack: &lt;code&gt;[rsp]&lt;/code&gt; &lt;em&gt;is&lt;/em&gt; &lt;code&gt;argc&lt;/code&gt;, &lt;code&gt;ret&lt;/code&gt; pops this small integer into &lt;code&gt;rip&lt;/code&gt;, and you end up debugging your assembly skills. So yes, skip the &lt;code&gt;ret&lt;/code&gt;: you were jumped to from the kernel, not called, so there's no need to act like a function.&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-nasm" data-lang="nasm"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;; RSP -&amp;gt; argc. Falls through with RAX = &amp;amp;auxv[0]&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;mov&lt;/span&gt; &lt;span class="nb"&gt;rcx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;; argc&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;lea&lt;/span&gt; &lt;span class="nb"&gt;rsi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;rsp&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nb"&gt;rcx&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;; &amp;amp;envp[0]: skip argc, argv[0..argc-1] and its NULL&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nl"&gt;.skip_env:&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;lodsq&lt;/span&gt; &lt;span class="c1"&gt;; DF is clear at process entry (SysV ABI)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;test&lt;/span&gt; &lt;span class="nb"&gt;rax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;rax&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;jnz&lt;/span&gt; &lt;span class="nv"&gt;.skip_env&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;mov&lt;/span&gt; &lt;span class="nb"&gt;rax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;rsi&lt;/span&gt; &lt;span class="c1"&gt;; &amp;amp;auxv[0]&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="c1"&gt;; ... keep going, RAX is live&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;The math is simple: &lt;code&gt;rsp&lt;/code&gt; points to &lt;code&gt;argc&lt;/code&gt;, so &lt;code&gt;argv&lt;/code&gt; starts at &lt;code&gt;rsp+8&lt;/code&gt;. Then there are &lt;code&gt;argc&lt;/code&gt; pointers plus a final &lt;code&gt;NULL&lt;/code&gt; (or &lt;code&gt;(argc+1)*8&lt;/code&gt; bytes), so in the end, &lt;code&gt;envp[0]&lt;/code&gt; is at &lt;code&gt;rsp + 8 + (argc+1)*8 = rsp + argc*8 + 16&lt;/code&gt;.&lt;/p&gt;&#10;&lt;h2 id="method-6-from-envp-or-environ-in-c"&gt;Method 6: from &lt;code&gt;envp&lt;/code&gt; or &lt;code&gt;environ&lt;/code&gt;, in C&lt;/h2&gt;&#10;&lt;p&gt;The same idea as in Method 5, but in C, when you already have &lt;code&gt;envp&lt;/code&gt; from &lt;code&gt;main&lt;/code&gt; (or &lt;code&gt;__libc_start_main&lt;/code&gt; passed it to you):&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;Elf64_auxv_t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nf"&gt;get_auxv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;envp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;envp&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;envp&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Elf64_auxv_t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="n"&gt;envp&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;There is also an official shortcut: if you don't have &lt;code&gt;envp&lt;/code&gt;, glibc exports the global &lt;code&gt;environ&lt;/code&gt; (&lt;code&gt;man 7 environ&lt;/code&gt;), so the code is the same. Well, that's &lt;em&gt;mostly&lt;/em&gt; true, but there's one thing.&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;environ&lt;/code&gt; points to the kernel's on-stack &lt;code&gt;envp&lt;/code&gt; only until you add the first brand new variable. &lt;code&gt;setenv&lt;/code&gt;/&lt;code&gt;putenv&lt;/code&gt; (&lt;a href="https://github.com/gnutools/glibc/blob/release/2.44/master/stdlib/setenv.c" target="_blank" rel="noopener"&gt;&lt;code&gt;stdlib/setenv.c&lt;/code&gt;&lt;/a&gt;) can't grow the array the kernel left on the stack, so on the first add, glibc &lt;a href="https://github.com/gnutools/glibc/blob/release/2.44/master/stdlib/setenv.c#L193" target="_blank" rel="noopener"&gt;copies it into its own heap array&lt;/a&gt; and &lt;a href="https://github.com/gnutools/glibc/blob/release/2.44/master/stdlib/setenv.c#L277" target="_blank" rel="noopener"&gt;points &lt;code&gt;environ&lt;/code&gt; to it&lt;/a&gt;. Overwriting an existing variable doesn't grow the array, so &lt;code&gt;environ&lt;/code&gt; stays where it was. And once it's on the heap, glibc allocates some extra space just in case and reuses that array, so it doesn't have to move again. But it never goes back to the stack. From that moment, &lt;code&gt;environ&lt;/code&gt; no longer points to the initial stack, the &lt;code&gt;auxv&lt;/code&gt; is nowhere near it, and the innocent-looking &lt;code&gt;get_auxv(environ)&lt;/code&gt; walks into the heap and returns garbage:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;at start: environ=0x7ffc4adb3cf8 (stack) get_auxv(environ) AT_PAGESZ=4096 [truth=4096]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;after overwrite: environ=0x7ffc4adb3cf8 (stack) get_auxv(environ) AT_PAGESZ=4096 [truth=4096]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;after add-new: environ=0x55666d24c6f0 (HEAP) get_auxv(environ) AT_PAGESZ=0 [truth=4096]&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;So use the &lt;code&gt;envp&lt;/code&gt; argument from &lt;code&gt;main&lt;/code&gt;/&lt;code&gt;_start&lt;/code&gt;, which still points to the original array on the stack. Fall back to &lt;code&gt;environ&lt;/code&gt; only if you have to, and only before anyone has added a new variable.&lt;/p&gt;&#10;&lt;p&gt;There's an even nastier version of this for setuid binaries, and it's related to &lt;a href="#method-1-ld_show_auxv1-the-shell-one-liner"&gt;Method 1&lt;/a&gt;. When &lt;code&gt;ld.so&lt;/code&gt; removes the &lt;code&gt;LD_*&lt;/code&gt; variables for a secure exec, it uses &lt;code&gt;unsetenv&lt;/code&gt;, which in glibc sadly compacts the array in place: &lt;a href="https://github.com/gnutools/glibc/blob/release/2.44/master/stdlib/setenv.c#L322" target="_blank" rel="noopener"&gt;it shifts the remaining pointers down&lt;/a&gt; and writes an earlier &lt;code&gt;NULL&lt;/code&gt; terminator, leaving stale slots between this new &lt;code&gt;NULL&lt;/code&gt; and the real start of the &lt;code&gt;auxv&lt;/code&gt;. After that, &amp;quot;walk &lt;code&gt;envp&lt;/code&gt; to the &lt;code&gt;NULL&lt;/code&gt; and get the next entry&amp;quot; fails and lands on a stale slot, not on &lt;code&gt;auxv[0]&lt;/code&gt;. Try running a root-owned setuid binary as a normal user with &lt;code&gt;LD_SHOW_AUXV&lt;/code&gt; and &lt;code&gt;LD_LIBRARY_PATH&lt;/code&gt; in its environment, and &lt;code&gt;get_auxv(envp)&lt;/code&gt; will return an &lt;code&gt;auxv&lt;/code&gt; whose first entry is &lt;code&gt;AT_NULL&lt;/code&gt;, i.e. an empty &lt;code&gt;auxv&lt;/code&gt;. But &lt;code&gt;/proc/self/auxv&lt;/code&gt;, &lt;code&gt;getauxval&lt;/code&gt; and the stack scanner still find the real one 16 bytes higher. So if you're reversing something that runs setuid, don't blindly trust the &lt;code&gt;envp&lt;/code&gt; walk: it can give you an empty &lt;code&gt;auxv&lt;/code&gt;.&lt;/p&gt;&#10;&lt;p&gt;But this method still has one big advantage: you can run it before &lt;code&gt;main&lt;/code&gt;. &lt;code&gt;get_auxv&lt;/code&gt; only needs an &lt;code&gt;envp&lt;/code&gt;, and a function in the executable's &lt;code&gt;.init_array&lt;/code&gt; gets one before &lt;code&gt;main&lt;/code&gt; even starts. glibc calls every constructor with &lt;code&gt;(int argc, char **argv, char **envp)&lt;/code&gt; (see &lt;code&gt;call_init&lt;/code&gt; in &lt;a href="https://github.com/gnutools/glibc/blob/release/2.44/master/csu/libc-start.c#L144" target="_blank" rel="noopener"&gt;&lt;code&gt;csu/libc-start.c&lt;/code&gt;&lt;/a&gt;, where the array has this nice-looking type: &lt;code&gt;void (*[])(int, char **, char **)&lt;/code&gt;). So a constructor like the one below is one of the earliest points in a normal (shellcode-free) program where you can read the &lt;code&gt;auxv&lt;/code&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nf"&gt;__attribute__&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;peek_auxv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;argc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;envp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;Elf64_auxv_t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;av&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;get_auxv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;envp&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// the walker above&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="c1"&gt;// inspect av before main(): AT_SECURE, AT_HWCAP, AT_RANDOM, ...&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;.preinit_array&lt;/code&gt; runs even earlier, before the normal constructors and with the same signature. But only in the main executable, never in a shared library.&lt;/p&gt;&#10;&lt;p&gt;Both run before &lt;code&gt;main&lt;/code&gt;, and both get the original &lt;code&gt;envp&lt;/code&gt; that is still on the stack (glibc gives those constructors &lt;a href="https://github.com/gnutools/glibc/blob/release/2.44/master/csu/libc-start.c#L359" target="_blank" rel="noopener"&gt;the global &lt;code&gt;__environ&lt;/code&gt;&lt;/a&gt;). So the &lt;code&gt;setenv&lt;/code&gt; trap doesn't apply here: no user code has run yet, so nobody could have moved your &lt;code&gt;environ&lt;/code&gt; to the heap. But the setuid case above still applies: &lt;code&gt;ld.so&lt;/code&gt; removes &lt;code&gt;LD_*&lt;/code&gt; during its own startup (&lt;code&gt;dl_main&lt;/code&gt; calls &lt;code&gt;process_envvars_secure&lt;/code&gt;, which calls &lt;code&gt;unsetenv&lt;/code&gt;, all before you even &lt;code&gt;_start&lt;/code&gt;), and as you already know, &lt;code&gt;unsetenv&lt;/code&gt; compacts the array in place instead of reallocating it. So on an &lt;code&gt;AT_SECURE&lt;/code&gt; binary, the &lt;code&gt;envp&lt;/code&gt; walk in a constructor hits the same stale &lt;code&gt;NULL&lt;/code&gt; and fails in the same way as in &lt;code&gt;main&lt;/code&gt;. Don't panic, you still have &lt;code&gt;/proc&lt;/code&gt;, &lt;code&gt;getauxval&lt;/code&gt;, &lt;code&gt;prctl&lt;/code&gt; or the stack scanner for those.&lt;/p&gt;&#10;&lt;p&gt;Passing &lt;code&gt;(argc, argv, envp)&lt;/code&gt; to constructors is actually glibc's own invention, not part of the C standard. So if you care about musl: &lt;a href="https://git.musl-libc.org/cgit/musl/tree/ldso/dynlink.c?h=v1.2.6#n1606" target="_blank" rel="noopener"&gt;it calls &lt;code&gt;.init_array&lt;/code&gt; entries without any arguments&lt;/a&gt; (the same goes for &lt;a href="https://git.musl-libc.org/cgit/musl/tree/src/env/__libc_start_main.c?h=v1.2.6#n64" target="_blank" rel="noopener"&gt;static binaries&lt;/a&gt;), and the three-argument constructor trick gets garbage there. On musl, use &lt;code&gt;environ&lt;/code&gt; inside the constructor instead (it's already valid that early, yes), or just call &lt;code&gt;getauxval&lt;/code&gt;.&lt;/p&gt;&#10;&lt;h2 id="method-7-scan-the-stack-for-known-invariants"&gt;Method 7: scan the stack for known invariants&lt;/h2&gt;&#10;&lt;p&gt;The last resort. No &lt;code&gt;argc&lt;/code&gt;, no &lt;code&gt;envp&lt;/code&gt;, no libc, no &lt;code&gt;/proc&lt;/code&gt;, no &lt;code&gt;prctl&lt;/code&gt;: you just have a pointer somewhere on the main thread's stack, and you badly need the &lt;code&gt;auxv&lt;/code&gt;. Easy: it's near the top of that stack, so you only need to scan upward from wherever you are and recognize it by its content.&lt;/p&gt;&#10;&lt;p&gt;How do you recognize it? Two entries have the same value in every process on x86-64: &lt;code&gt;AT_PAGESZ&lt;/code&gt; is always &lt;code&gt;0x1000&lt;/code&gt; (4 KiB pages) and &lt;code&gt;AT_CLKTCK&lt;/code&gt; is always &lt;code&gt;100&lt;/code&gt; (&lt;code&gt;USER_HZ&lt;/code&gt;). These two values plus the edges of the &lt;code&gt;auxv&lt;/code&gt; (the &lt;code&gt;envp&lt;/code&gt; &lt;code&gt;NULL&lt;/code&gt; right before &lt;code&gt;auxv[0]&lt;/code&gt; and a clean &lt;code&gt;AT_NULL&lt;/code&gt; at the end) make a fingerprint that almost never appears by accident.&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stddef.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdint.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;elf.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cp"&gt;#define PAGESZ 0x1000 &lt;/span&gt;&lt;span class="cm"&gt;/* x86-64 base pages */&lt;/span&gt;&lt;span class="cp"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cp"&gt;#define CLKTCK 100 &lt;/span&gt;&lt;span class="cm"&gt;/* USER_HZ */&lt;/span&gt;&lt;span class="cp"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cp"&gt;#define STACK_TOP 0x00007ffffffff000UL &lt;/span&gt;&lt;span class="cm"&gt;/* DEFAULT_MAP_WINDOW = (1&amp;lt;&amp;lt;47) - PAGE_SIZE */&lt;/span&gt;&lt;span class="cp"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cm"&gt;/* Is p the real &amp;amp;auxv[0]? An auxv entry is just two 64-bit words. */&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;is_auxv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;uint64_t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="cm"&gt;/* envp&amp;#39;s NULL sits just below auxv[0] */&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;anchors&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* each entry: { a_type, a_val } */&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="kt"&gt;uint64_t&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;AT_NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="cm"&gt;/* clean terminator + both anchors */&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;anchors&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;AT_MINSIGSTKSZ&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="cm"&gt;/* 51 = largest real tag */&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;anchors&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;AT_PAGESZ&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;PAGESZ&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;anchors&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;AT_CLKTCK&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;CLKTCK&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cm"&gt;/* Scan the main thread&amp;#39;s stack upward from any address below the auxv. */&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;Elf64_auxv_t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nf"&gt;find_auxv_by_scan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="cm"&gt;/* auxv[0] is 8-byte aligned (not necessarily 16): round down, step one word. */&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint64_t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint64_t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)((&lt;/span&gt;&lt;span class="kt"&gt;uintptr_t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uintptr_t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uintptr_t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;STACK_TOP&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;is_auxv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Elf64_auxv_t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Call it with the address of any local variable (&lt;code&gt;int x; find_auxv_by_scan(&amp;amp;x);&lt;/code&gt;), and it returns &lt;code&gt;&amp;amp;auxv[0]&lt;/code&gt;.&lt;/p&gt;&#10;&lt;p&gt;What makes it work:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;scan upward: I suppose you remember that the stack grows down, which means your frame is &lt;em&gt;below&lt;/em&gt; the &lt;code&gt;auxv&lt;/code&gt; the kernel built near the top. So you need to walk toward higher addresses.&lt;/li&gt;&#10;&lt;li&gt;8-byte alignment: &lt;code&gt;auxv[0]&lt;/code&gt; is only guaranteed to be 8-byte aligned, not 16. (&lt;a href="https://github.com/torvalds/linux/blob/v6.18/fs/binfmt_elf.c#L302-L303" target="_blank" rel="noopener"&gt;The 16-byte aligned landmark on the stack is &lt;code&gt;argc&lt;/code&gt;&lt;/a&gt;, because &lt;a href="https://github.com/torvalds/linux/blob/v6.18/fs/binfmt_elf.c#L149-L150" target="_blank" rel="noopener"&gt;&lt;code&gt;STACK_ROUND&lt;/code&gt;&lt;/a&gt; rounds the stack pointer down to 16 bytes, and the &lt;code&gt;auxv&lt;/code&gt; is a variable number of 8-byte words above it, so it's 16-byte aligned only about half the time.) That's why the code rounds the start down to 8 and steps by 8: with a 16-byte step, it would jump right over &lt;code&gt;auxv[0]&lt;/code&gt; every time it isn't 16-byte aligned.&lt;/li&gt;&#10;&lt;li&gt;false-positive guards: scanning up, you cross C stack frames and the &lt;code&gt;argv&lt;/code&gt;/&lt;code&gt;envp&lt;/code&gt; pointer arrays, so some places will look &lt;code&gt;auxv&lt;/code&gt;-ish. A place counts as the real &lt;code&gt;auxv[0]&lt;/code&gt; only when the word right below it is &lt;code&gt;0&lt;/code&gt; (&lt;code&gt;envp&lt;/code&gt;'s terminating &lt;code&gt;NULL&lt;/code&gt;) &lt;em&gt;and&lt;/em&gt; walking forward from it reaches a clean &lt;code&gt;AT_NULL&lt;/code&gt; with sane tags (&lt;code&gt;&amp;lt;= AT_MINSIGSTKSZ&lt;/code&gt;), including &lt;code&gt;AT_PAGESZ&lt;/code&gt; and &lt;code&gt;AT_CLKTCK&lt;/code&gt; with their known values. Random stack data practically never passes all of that. The first check also helps with a classic issue: a program that copied the &lt;code&gt;auxv&lt;/code&gt; onto its stack (say, with a &lt;code&gt;read&lt;/code&gt; from &lt;code&gt;/proc/self/auxv&lt;/code&gt;) leaves a &lt;em&gt;copy&lt;/em&gt; lower down, and the scan finds it first. A copy usually isn't preceded by a &lt;code&gt;0&lt;/code&gt; word, so it's skipped. Note the &amp;quot;usually&amp;quot; (and the &amp;quot;practically&amp;quot;) above: for instance, a copy in a zero-initialized buffer with a &lt;code&gt;0&lt;/code&gt; right before it is accepted instead of the real &lt;code&gt;auxv&lt;/code&gt;.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;There is also a ceiling: the scan stops at &lt;code&gt;STACK_TOP&lt;/code&gt;, which is &lt;code&gt;0x00007ffffffff000&lt;/code&gt; on x86-64. It's not a random constant I chose: it's the kernel's own &lt;a href="https://github.com/torvalds/linux/blob/v6.18/arch/x86/include/asm/page_64_types.h#L54-L69" target="_blank" rel="noopener"&gt;&lt;code&gt;STACK_TOP&lt;/code&gt;&lt;/a&gt; macro (&lt;code&gt;= DEFAULT_MAP_WINDOW = (1&amp;lt;&amp;lt;47) - PAGE_SIZE&lt;/code&gt;), the value &lt;a href="https://github.com/torvalds/linux/blob/v6.18/fs/binfmt_elf.c#L1028" target="_blank" rel="noopener"&gt;&lt;code&gt;fs/binfmt_elf.c&lt;/code&gt;&lt;/a&gt; passes to &lt;code&gt;randomize_stack_top()&lt;/code&gt; when it lays out the stack. With ASLR on, this function subtracts a random page-aligned offset (up to 16 GiB on x86-64, where &lt;a href="https://github.com/torvalds/linux/blob/v6.18/arch/x86/include/asm/elf.h#L328-L329" target="_blank" rel="noopener"&gt;&lt;code&gt;STACK_RND_MASK&lt;/code&gt;&lt;/a&gt; is 22 bits of pages), and the &lt;code&gt;[stack]&lt;/code&gt; mapping ends at that random top, not at &lt;code&gt;STACK_TOP&lt;/code&gt;. The two are the same only with ASLR off (&lt;code&gt;setarch -R&lt;/code&gt;, or under gdb, which disables it by default), and that's the familiar &lt;code&gt;...-7ffffffff000 [stack]&lt;/code&gt; line in gdb. So &lt;code&gt;STACK_TOP&lt;/code&gt; is an upper bound on where the &lt;code&gt;auxv&lt;/code&gt; can be, not the end of the mapping. And it's not a safety net either: above the real top of &lt;code&gt;[stack]&lt;/code&gt; there is unmapped memory, so a scan that misses the &lt;code&gt;auxv&lt;/code&gt; crashes or walks into unrelated pages long before it reaches &lt;code&gt;STACK_TOP&lt;/code&gt;. On the main thread this never happens, because a correct scan finds the real &lt;code&gt;auxv&lt;/code&gt; inside the mapping first, and the ceiling only keeps the loop bounded on paper. If you need a precise bound, read the real end of &lt;code&gt;[stack]&lt;/code&gt; from &lt;code&gt;/proc/self/maps&lt;/code&gt;.&lt;/p&gt;&#10;&lt;p&gt;And one damn hard limitation: this only works on the main thread, unfortunately. A thread created with &lt;code&gt;clone&lt;/code&gt; runs on a new stack without &lt;code&gt;argc&lt;/code&gt;/&lt;code&gt;argv&lt;/code&gt;/&lt;code&gt;envp&lt;/code&gt;/&lt;code&gt;auxv&lt;/code&gt;, far below the &lt;code&gt;0x7fff...&lt;/code&gt; ceiling. So the scan would run past the top of that stack (into whatever is mapped above it, or into unmapped memory) long before it reaches &lt;code&gt;STACK_TOP&lt;/code&gt;. If you really need to do this from another thread, find the main thread's &lt;code&gt;[stack]&lt;/code&gt; in &lt;code&gt;/proc/self/maps&lt;/code&gt; and scan only inside it, starting from its lower end.&lt;/p&gt;&#10;&lt;p&gt;So it's really the last resort. The fingerprint is strong, and the &lt;code&gt;envp&lt;/code&gt; &lt;code&gt;NULL&lt;/code&gt; check helps with the usual copy problem, but it's still a heuristic over raw memory, and an unlucky (or adversarial) layout can fool it. If any of Methods 1-6 is available, use it instead.&lt;/p&gt;&#10;&lt;h2 id="when-the-api-can-lie"&gt;When the API can lie&lt;/h2&gt;&#10;&lt;p&gt;Remember the two copies of the &lt;code&gt;auxv&lt;/code&gt; from &lt;a href="#method-2-read-procselfauxv-directly"&gt;Method 2&lt;/a&gt;? &lt;code&gt;/proc/self/auxv&lt;/code&gt; and &lt;code&gt;prctl(PR_GET_AUXV)&lt;/code&gt; read the kernel's snapshot in &lt;code&gt;mm-&amp;gt;saved_auxv&lt;/code&gt;, while &lt;code&gt;getauxval&lt;/code&gt; reads the copy on the stack, which &lt;code&gt;ld.so&lt;/code&gt; can change. Usually they are the same, but there is one easy way to make them different: run the dynamic loader explicitly.&lt;/p&gt;&#10;&lt;p&gt;Normally you run &lt;code&gt;./prog&lt;/code&gt;: the kernel sees the &lt;code&gt;PT_INTERP&lt;/code&gt; entry, maps &lt;code&gt;ld.so&lt;/code&gt; for you, and fills the &lt;code&gt;auxv&lt;/code&gt; to describe &lt;code&gt;./prog&lt;/code&gt; (&lt;code&gt;AT_PHDR&lt;/code&gt;/&lt;code&gt;AT_PHNUM&lt;/code&gt;/&lt;code&gt;AT_ENTRY&lt;/code&gt; point to the program, and &lt;code&gt;AT_EXECFN&lt;/code&gt; is its path). But you can also run &lt;code&gt;ld.so ./prog&lt;/code&gt;, i.e. run the loader itself as the program, with &lt;code&gt;./prog&lt;/code&gt; as its argument. Now the executed file &lt;em&gt;is&lt;/em&gt; &lt;code&gt;ld.so&lt;/code&gt;, so the kernel fills the &lt;code&gt;auxv&lt;/code&gt; to describe &lt;code&gt;ld.so&lt;/code&gt; itself: &lt;code&gt;AT_PHDR&lt;/code&gt;/&lt;code&gt;AT_PHNUM&lt;/code&gt;/&lt;code&gt;AT_ENTRY&lt;/code&gt; point to the loader, and &lt;code&gt;AT_EXECFN&lt;/code&gt; is &lt;code&gt;/lib64/ld-linux-x86-64.so.2&lt;/code&gt;. When &lt;code&gt;ld.so&lt;/code&gt; finally loads &lt;code&gt;./prog&lt;/code&gt;, it makes the process look like &lt;code&gt;./prog&lt;/code&gt; was executed directly. To make it work, it walks the &lt;code&gt;auxv&lt;/code&gt; on the stack and rewrites (yes) those four tags (&lt;code&gt;AT_PHDR&lt;/code&gt;, &lt;code&gt;AT_PHNUM&lt;/code&gt;, &lt;code&gt;AT_ENTRY&lt;/code&gt;, &lt;code&gt;AT_EXECFN&lt;/code&gt;) to point to the real program. This loop is in &lt;code&gt;dl_main&lt;/code&gt; in &lt;a href="https://github.com/gnutools/glibc/blob/release/2.44/master/elf/rtld.c#L1610" target="_blank" rel="noopener"&gt;&lt;code&gt;elf/rtld.c&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;&#10;&lt;p&gt;But it patches only the copy on the stack. &lt;code&gt;mm-&amp;gt;saved_auxv&lt;/code&gt; is never touched, so &lt;code&gt;/proc/self/auxv&lt;/code&gt; and &lt;code&gt;PR_GET_AUXV&lt;/code&gt; still describe &lt;code&gt;ld.so&lt;/code&gt;, while &lt;code&gt;getauxval&lt;/code&gt;, which reads the patched copy on the stack, describes &lt;code&gt;./prog&lt;/code&gt;. Weird, I know. Same process, same tags, two different answers:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; /lib64/ld-linux-x86-64.so.2 ./ldso_auxv&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;argv[0] = ./ldso_auxv&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;tag /proc/self/auxv (kernel) on-stack (ld.so&amp;#39;s copy)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_SYSINFO_EHDR 33 0x7f7310c2f000 0x7f7310c2f000&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_MINSIGSTKSZ 51 1776 1776&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_HWCAP 16 0xf8bfbff 0xf8bfbff&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_PAGESZ 6 4096 4096&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_CLKTCK 17 100 100&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_PHDR 3 0x7f7310c31040 0x7f7310c26040 &amp;lt;- differs&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_PHENT 4 56 56&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_PHNUM 5 9 14 &amp;lt;- differs&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_BASE 7 0 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_FLAGS 8 0 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_ENTRY 9 0x7f7310c4e2c0 0x7f7310c27100 &amp;lt;- differs&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_UID 11 1000 1000&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_EUID 12 1000 1000&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_GID 13 1000 1000&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_EGID 14 1000 1000&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_SECURE 23 0 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_RANDOM 25 0x7fffc881c749 0x7fffc881c749&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_HWCAP2 26 2 2&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_EXECFN 31 /lib64/ld-linux-x86-64.so.2 ./ldso_auxv &amp;lt;- differs&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_PLATFORM 15 0x7fffc881c759 0x7fffc881c759&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_RSEQ_FEATURE_SIZE 27 28 28&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_RSEQ_ALIGN 28 32 32&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;4 of 22 tags differ&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;Run the same binary the normal way (&lt;code&gt;./prog&lt;/code&gt;), and the two copies match to the byte. In other words, the &lt;code&gt;auxv&lt;/code&gt; is actually two things: the kernel's snapshot and a copy on the stack that the loader is allowed to edit. And the method you choose decides which one you see.&lt;/p&gt;&#10;&lt;p&gt;Happy hacking!&lt;/p&gt;&#10;</description></item></channel></rss>