<div dir="ltr"><br><br><div class="gmail_quote"><div dir="ltr">On Tue, Dec 11, 2018 at 11:57 AM Pavel Labath <<a href="mailto:pavel@labath.sk">pavel@labath.sk</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">The part I know nothing about is whether something similar could be done <br>
for PE/COFF files (and I'll need something like that there too). Adrian, <br>
Zachary, what is the relation ship between "image base" of an object <br>
file and its sections? Is there any way we could arrange so that the <br>
base address of a module always belongs to one of its sections?<br><br></blockquote><div><br></div><div>Historically, an image base of N was used as a way to tell the loader "map the file in so that byte 0 of the file is virtual address N in the process's address space".  as in *((char *)N) would be the first byte of the file in a running process.  Then, everything else in the file is written as an offset from N.  This includes section addresses.  So for example, if we use dumpbin on a simple executable we can see something like this:</div><div><br></div><div><div>Dump of file bin\count.exe</div><div><br></div><div>PE signature found</div><div><br></div><div>File Type: EXECUTABLE IMAGE</div><div><br></div><div>OPTIONAL HEADER VALUES<br></div><div>                  ...</div><div>       140000000 image base (0000000140000000 to 0000000140011FFF)<br></div></div><div>                  ...</div><div><div>SECTION HEADER #1</div><div>   .text name</div><div>    1000 virtual address (0000000140001000 to 00000001400089AE)<br></div><div><br></div></div><div>So under this scheme, the first byte of the first section would be at virtual address 0000000140001000 in the running process.</div><div><br></div><div>Later, ASLR came along and threw a wrench in all of that, and so these days the image base is mostly meaningless.  The loader will probably never actually load your module at the address specified in image base.  But the rest of the rules still hold true.  Wherever it *does* load your module, the first byte of .text will still be at offset 1000 from that.<br></div><div><br></div><div>So, if you want to return this value from the PE/COFF header, or even if you want to return the actual address that the module was loaded at, then no, it will never belong to any section (because the bytes at that address will be the PE/COFF file header).</div><div><br></div><div>Does this make sense?</div></div></div>