mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-06 03:08:31 +00:00
Introduce post-linker that converts ELF to PE
If you build a static ELF executable in `ld -q` mode (which leaves rela sections inside the binary) then you can run it through the elf2pe.com program afterwards, which will turn it into a PE executable. We have a new trick for defining WIN32 DLL imports in C without any assembly code. This also achieves the optimally tiny and perfect PE binary structure. We need this because it isn't possible to have a GNU ld linker script generate a PE file where the virtual pointer and the file pointer can drift apart. This post-linker can do that. One cool benefit is we can now use a smaller 512-byte alignment in the file, and an even bigger 64kb alignment for the segment virtual addresses, and the executable ends up being smaller. Another program introduced by this change is pecheck.com which can do extensive linting of PE static executables to help explain why Windows won't load it.
This commit is contained in:
parent
2f35bbf046
commit
dd53f31147
18 changed files with 1914 additions and 102 deletions
|
@ -27,6 +27,7 @@
|
|||
#include "libc/nt/struct/imagentheaders.internal.h"
|
||||
#include "libc/nt/struct/imageoptionalheader.internal.h"
|
||||
#include "libc/nt/struct/imagesectionheader.internal.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/map.h"
|
||||
|
@ -45,9 +46,32 @@
|
|||
*/
|
||||
|
||||
static const char *path;
|
||||
|
||||
static struct NtImageDosHeader *mz;
|
||||
static size_t mzsize;
|
||||
|
||||
static struct NtImageSectionHeader *sections;
|
||||
static size_t section_count;
|
||||
|
||||
static void *GetOff(uint32_t off) {
|
||||
if (off < mzsize) return (char *)mz + off;
|
||||
fprintf(stderr, "%s: off %#x not defined within image\n", path, off);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
static void *GetRva(uint32_t rva) {
|
||||
int i;
|
||||
for (i = 0; i < section_count; ++i) {
|
||||
if (sections[i].VirtualAddress <= rva &&
|
||||
rva < sections[i].VirtualAddress + sections[i].Misc.VirtualSize) {
|
||||
return (char *)mz + sections[i].PointerToRawData +
|
||||
(rva - sections[i].VirtualAddress);
|
||||
}
|
||||
}
|
||||
fprintf(stderr, "%s: rva %#x not defined by any sections\n", path, rva);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
static struct XedDecodedInst *ildreal(void *addr) {
|
||||
static struct XedDecodedInst xedd;
|
||||
if (xed_instruction_length_decode(
|
||||
|
@ -67,20 +91,13 @@ static void startfile(void) {
|
|||
|
||||
static void *pecheckaddress(struct NtImageDosHeader *mz, size_t mzsize,
|
||||
void *addr, uint32_t addrsize) {
|
||||
#if !(TRUSTWORTHY + PE_TRUSTWORTHY + 0)
|
||||
if ((intptr_t)addr < (intptr_t)mz ||
|
||||
(intptr_t)addr + addrsize > (intptr_t)mz + mzsize) {
|
||||
abort();
|
||||
}
|
||||
#endif
|
||||
return addr;
|
||||
}
|
||||
|
||||
static void *PeComputeRva(struct NtImageDosHeader *mz, size_t mzsize,
|
||||
uint32_t reladdr, uint32_t addrsize) {
|
||||
return pecheckaddress(mz, mzsize, (void *)((intptr_t)mz + reladdr), addrsize);
|
||||
}
|
||||
|
||||
static void showmzheader(void) {
|
||||
showtitle(basename(path), "dos", "mz header",
|
||||
"\tMZ = Mark 'Zibo' Joseph Zbikowski\n"
|
||||
|
@ -217,10 +234,10 @@ static void ShowIlt(int64_t *ilt) {
|
|||
show(".quad", format(b1, "%#lx", *ilt),
|
||||
_gc(xasprintf("@%#lx", (intptr_t)ilt - (intptr_t)mz)));
|
||||
if (*ilt) {
|
||||
char *hint = (char *)((intptr_t)mz + *ilt);
|
||||
char *hint = GetRva(*ilt);
|
||||
printf("/\t.short\t%d\t\t\t# @%#lx\n", READ16LE(hint),
|
||||
(intptr_t)hint - (intptr_t)mz);
|
||||
char *name = (char *)((intptr_t)mz + *ilt + 2);
|
||||
char *name = GetRva(*ilt + 2);
|
||||
printf("/\t.asciz\t%`'s\n", name);
|
||||
printf("/\t.align\t2\n");
|
||||
}
|
||||
|
@ -240,14 +257,14 @@ static void ShowIat(char *iat, size_t size) {
|
|||
show(".long", format(b1, "%#x", READ32LE(p + 8)), "ForwarderChain");
|
||||
show(".long", format(b1, "%#x", READ32LE(p + 12)),
|
||||
READ32LE(p + 12)
|
||||
? _gc(xasprintf("DllName RVA (%s)", (char *)mz + READ32LE(p + 12)))
|
||||
? _gc(xasprintf("DllName RVA (%s)", GetRva(READ32LE(p + 12))))
|
||||
: "DllName RVA");
|
||||
show(".long", format(b1, "%#x", READ32LE(p + 16)),
|
||||
"ImportAddressTable RVA");
|
||||
}
|
||||
for (p = iat, e = iat + size; p + 20 <= e; p += 20) {
|
||||
if (READ32LE(p)) {
|
||||
ShowIlt((void *)((intptr_t)mz + READ32LE(p)));
|
||||
ShowIlt(GetRva(READ32LE(p)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -286,6 +303,8 @@ static void ShowSection(struct NtImageSectionHeader *s) {
|
|||
|
||||
static void ShowSections(struct NtImageSectionHeader *s, size_t n) {
|
||||
size_t i;
|
||||
sections = s;
|
||||
section_count = n;
|
||||
printf("\n");
|
||||
showtitle(basename(path), "windows", "sections", 0, 0);
|
||||
for (i = 0; i < n; ++i) {
|
||||
|
@ -323,26 +342,25 @@ static void showpeheader(struct NtImageNtHeaders *pe) {
|
|||
pe->FileHeader.NumberOfSections *
|
||||
sizeof(struct NtImageSectionHeader)),
|
||||
pe->FileHeader.NumberOfSections);
|
||||
ShowIat(
|
||||
(void *)((intptr_t)mz +
|
||||
pe->OptionalHeader.DataDirectory[kNtImageDirectoryEntryImport]
|
||||
.VirtualAddress),
|
||||
pe->OptionalHeader.DataDirectory[kNtImageDirectoryEntryImport].Size);
|
||||
ShowIat(GetRva(pe->OptionalHeader.DataDirectory[kNtImageDirectoryEntryImport]
|
||||
.VirtualAddress),
|
||||
pe->OptionalHeader.DataDirectory[kNtImageDirectoryEntryImport].Size);
|
||||
}
|
||||
|
||||
static void showall(void) {
|
||||
|
||||
startfile();
|
||||
showmzheader();
|
||||
showdosstub();
|
||||
if (mz->e_lfanew) {
|
||||
showpeheader(PeComputeRva(mz, mzsize, mz->e_lfanew,
|
||||
sizeof(struct NtImageFileHeader)));
|
||||
showpeheader(GetOff(mz->e_lfanew));
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int64_t fd;
|
||||
struct stat st[1];
|
||||
ShowCrashReports();
|
||||
if (argc != 2) fprintf(stderr, "usage: %s FILE\n", argv[0]), exit(1);
|
||||
if ((fd = open((path = argv[1]), O_RDONLY)) == -1 || fstat(fd, st) == -1 ||
|
||||
(mz = mmap(NULL, (mzsize = st->st_size), PROT_READ, MAP_SHARED, fd, 0)) ==
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue