diff options
| author | 2023-06-17 23:26:45 +0200 | |
|---|---|---|
| committer | 2023-06-17 23:26:45 +0200 | |
| commit | 962ad175aee634274b408ead38b13e6bc90e2fe7 (patch) | |
| tree | cfda733adf56ed12e829f594e0b6d66f0a1a7a70 /obj.c | |
| parent | ec28e9057e84b92acabb7ebf9122af59738917ad (diff) | |
basic ELF output
Diffstat (limited to 'obj.c')
| -rw-r--r-- | obj.c | 53 |
1 files changed, 46 insertions, 7 deletions
@@ -1,27 +1,66 @@ #include "obj.h" #include "common.h" +#include "ir.h" +#include <errno.h> +#include <fcntl.h> +#include <unistd.h> + +void elfinit(void); +void elfaddsym(const char *, int info, enum section, uvlong value, uvlong size); +void elfreloc(const char *sym, enum relockind, enum section, uint off, vlong addend); +void elffini(struct wbuf *); struct objfile objout; +enum { NTEXT = 4<<20 /* 4MiB */ }; + void objini(const char *file) { - enum { NTEXT = 4<<20 /* 4MiB */ }; assert(!objout.file); objout.file = file; objout.code = objout.textbegin = mapzeros(NTEXT); objout.textend = objout.textbegin + NTEXT; + + switch (mctarg->objkind) { + case OBJELF: elfinit(); break; + } +} + +void +objdeffunc(const char *nam, bool globl, uint off, uint siz) +{ + switch (mctarg->objkind) { + case OBJELF: + elfaddsym(nam, /*STT_LOCAL/GLOBAL*/globl << 4 | /*STT_FUNC*/2, Stext, off, siz); + break; + } +} + +void +objreloc(const char *sym, enum relockind reloc, enum section section, uint off, vlong addend) +{ + switch (mctarg->objkind) { + case OBJELF: + elfreloc(sym, reloc, section, off, addend); + break; + } } void objfini(void) { - void *popen(char *, char *), pclose(void *); - long fwrite(void *, size_t, size_t, void *); - void *cmd = popen("ndisasm -b64 -", "w"); - ioflush(&bstderr); ioflush(&bstdout); - fwrite(objout.textbegin, 1, objout.code - objout.textbegin, cmd); - pclose(cmd); + static char buf[1<<10]; + struct wbuf out = FDBUF(buf, sizeof buf, open(objout.file, O_WRONLY | O_CREAT | O_TRUNC, 0666)); + if (out.fd < 0) fatal(NULL, "could not open %'s for writing: %s", objout.file, strerror(errno)); + + switch (mctarg->objkind) { + case OBJELF: elffini(&out); break; + } + + munmap(objout.textbegin, NTEXT); + ioflush(&out); + close(out.fd); } /* vim:set ts=3 sw=3 expandtab: */ |