From 75da6e3498f7ddc4f701fac77a3d8b9d6e96ca8d Mon Sep 17 00:00:00 2001 From: Andrew Chambers Date: Sat, 16 Oct 2021 12:48:28 +1300 Subject: Fix bug in xchg. --- main.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index 86721e8..34c2f82 100644 --- a/main.c +++ b/main.c @@ -1,5 +1,4 @@ #include "minias.h" -#include /* Parsed assembly */ static AsmLine *allasm = NULL; @@ -29,7 +28,7 @@ static Section *datarel = NULL; static char *infilename = ""; static size_t curlineno = 0; -void lfatal(const char *fmt, ...) { +static void lfatal(const char *fmt, ...) { va_list ap; fprintf(stderr, "%s:%ld: ", infilename, curlineno); va_start(ap, fmt); @@ -155,7 +154,7 @@ static void initsections(void) { datarel->hdr.sh_entsize = sizeof(Elf64_Rela); } -Relocation *newreloc() { +static Relocation *newreloc() { if (nrelocs == reloccap) { reloccap = nrelocs ? nrelocs * 2 : 64; relocs = xreallocarray(relocs, reloccap, sizeof(Relocation)); @@ -559,7 +558,7 @@ static void assemblexchg(const Instr *xchg) { rex = (Rex){ .required = isrexreg(xchg->arg1->kind) || isrexreg(xchg->arg2->kind), .w = isreg64(xchg->arg1->kind) || isreg64(xchg->arg2->kind), - .r = !!(regbits(reg) & (1 << 3)), + .b = !!(regbits(reg) & (1 << 3)), }; assembleplusr(rex, prefix, opcode, regbits(reg)); } else { -- cgit v1.2.3 From e141426d93b5ce975467263d157d2387a0913eea Mon Sep 17 00:00:00 2001 From: Andrew Chambers Date: Sat, 16 Oct 2021 14:17:38 +1300 Subject: Tidy. --- main.c | 80 +++++++++++++++++++++++++++++++++++------------------------------ parse.c | 3 ++- 2 files changed, 45 insertions(+), 38 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index 34c2f82..9ad714b 100644 --- a/main.c +++ b/main.c @@ -739,6 +739,45 @@ static void assembleset(const Instr *instr) { } } +static void assemblecall(const Call *call) { + Rex rex; + uint8_t rm; + + if (call->indirect) { + if (call->target.indirect->kind == ASM_MEMARG) { + rex = (Rex){0}; + assemblemem(&call->target.indirect->memarg, rex, -1, 0xff, 0x02); + } else { + rm = regbits(call->target.indirect->kind); + rex = (Rex){.b = !!(rm & (1 << 3))}; + assemblemodregrm(rex, -1, 0xff, 0x03, 0x02, rm); + } + } else { + sb(0xe8); + assemblereloc(call->target.direct.l, call->target.direct.c - 4, 4, + R_X86_64_PC32); + } +} + +static void assembleimul(const Instr *instr) { + VarBytes prefix, opcode; + + if (instr->variant < 8) { + assembledivmulneg(instr, 0x05); + } else if (instr->variant < 14) { + opcode = 0x01000faf; + prefix = ((instr->variant - 8) % 3) == 0 ? 0x66 : -1; + assemblerrm(instr, prefix, opcode, 1); + } else { + const Imm *imm; + imm = &instr->arg3->imm; + opcode = 0x69; + prefix = ((instr->variant - 14) % 3) == 0 ? 0x66 : -1; + assemblerrm(instr, prefix, opcode, 1); + assemblereloc(imm->v.l, imm->v.c, imm->nbytes, R_X86_64_32); + } +} + static void assemble(void) { Symbol *sym; AsmLine *l; @@ -845,26 +884,9 @@ static void assemble(void) { lfatal("%s already defined", sym->name); sym->defined = 1; break; - case ASM_CALL: { - Rex rex; - uint8_t rm; - - if (v->call.indirect) { - if (v->call.target.indirect->kind == ASM_MEMARG) { - rex = (Rex){0}; - assemblemem(&v->call.target.indirect->memarg, rex, -1, 0xff, 0x02); - } else { - rm = regbits(v->call.target.indirect->kind); - rex = (Rex){.b = !!(rm & (1 << 3))}; - assemblemodregrm(rex, -1, 0xff, 0x03, 0x02, rm); - } - } else { - sb(0xe8); - assemblereloc(v->call.target.direct.l, v->call.target.direct.c - 4, 4, - R_X86_64_PC32); - } + case ASM_CALL: + assemblecall(&v->call); break; - } case ASM_JMP: { static uint8_t variant2op[31] = { 0xe9, 0x84, 0x88, 0x8b, 0x8a, 0x8a, 0x80, 0x85, 0x89, 0x8b, 0x81, @@ -1052,25 +1074,9 @@ static void assemble(void) { case ASM_MULSS: assemblerrm(&v->instr, 0xf3, 0x01000f59, 1); break; - case ASM_IMUL: { - VarBytes prefix, opcode; - - if (v->instr.variant < 8) { - assembledivmulneg(&v->instr, 0x05); - } else if (v->instr.variant < 14) { - opcode = 0x01000faf; - prefix = ((v->instr.variant - 8) % 3) == 0 ? 0x66 : -1; - assemblerrm(&v->instr, prefix, opcode, 1); - } else { - const Imm *imm; - imm = &v->instr.arg3->imm; - opcode = 0x69; - prefix = ((v->instr.variant - 14) % 3) == 0 ? 0x66 : -1; - assemblerrm(&v->instr, prefix, opcode, 1); - assemblereloc(imm->v.l, imm->v.c, imm->nbytes, R_X86_64_32); - } + case ASM_IMUL: + assembleimul(&v->instr); break; - } case ASM_NEG: assembledivmulneg(&v->instr, 0x03); break; diff --git a/parse.c b/parse.c index a9d7b70..1634bb6 100644 --- a/parse.c +++ b/parse.c @@ -10,7 +10,8 @@ static const Parsev *internparsev(Parsev *p) { This simplicity comes with one big cost - Parsev variants with padding can trigger a false positive on valgrind. It should still be safe, - but the best fix is still to avoid the padding bytes in the Parsev variants. + but the best fix is still to avoid the padding bytes in the Parsev + variants. */ size_t idx; const Parsev *interned; -- cgit v1.2.3 From 7230c83abcf764da504b69b3433d3c9c1668e262 Mon Sep 17 00:00:00 2001 From: Andrew Chambers Date: Sat, 16 Oct 2021 14:53:48 +1300 Subject: More documentation. --- README.md | 1 + main.c | 17 ++++++++--------- 2 files changed, 9 insertions(+), 9 deletions(-) (limited to 'main.c') diff --git a/README.md b/README.md index 0369c31..ce41752 100644 --- a/README.md +++ b/README.md @@ -68,5 +68,6 @@ Bonus features: - [intel reference](https://software.intel.com/content/dam/develop/external/us/en/documents-tps/325383-sdm-vol-2abcd.pdf) - Specifically chapter 2.1 and chapter 3.1. - [elf spec](https://refspecs.linuxfoundation.org/elf/elf.pdf) +- [osdev wiki](https://wiki.osdev.org/X86-64_Instruction_Encoding) - [goas](https://github.com/DQNEO/goas) - [neatas](https://repo.or.cz/neatas.git) diff --git a/main.c b/main.c index 9ad714b..212e320 100644 --- a/main.c +++ b/main.c @@ -207,15 +207,7 @@ static uint8_t regbits(AsmKind k) { return (k - (ASM_REG_BEGIN + 1)) % 16; } static uint8_t isreg64(AsmKind k) { return k >= ASM_RAX && k <= ASM_R15; } -/* Register that requires the use of a rex prefix. */ -static uint8_t isrexreg(AsmKind k) { - return k > ASM_REG_BEGIN && k < ASM_REG_END && - (regbits(k) & (1 << 3) || k == ASM_SPL || k == ASM_BPL || - k == ASM_SIL || k == ASM_DIL); -} - -/* Compose a rex prefix - See intel manual. */ - +/* Rex opcode prefix. */ typedef struct Rex { uint8_t required : 1; uint8_t w : 1; @@ -224,6 +216,13 @@ typedef struct Rex { uint8_t b : 1; } Rex; +/* Register that requires the use of a rex prefix. */ +static uint8_t isrexreg(AsmKind k) { + return k > ASM_REG_BEGIN && k < ASM_REG_END && + (regbits(k) & (1 << 3) || k == ASM_SPL || k == ASM_BPL || + k == ASM_SIL || k == ASM_DIL); +} + static uint8_t rexbyte(Rex rex) { return ((1 << 6) | (rex.w << 3) | (rex.r << 2) | (rex.x << 1) | rex.b); } -- cgit v1.2.3 From 5405a8654b7474d45b0b3f34f83a375a3a418f46 Mon Sep 17 00:00:00 2001 From: Andrew Chambers Date: Sun, 17 Oct 2021 14:57:04 +1300 Subject: Implement simple jump relaxing. --- main.c | 114 ++++++++++++++++++++++++++++++++++++++++++++++++----------- minias.h | 1 + test/test.sh | 13 ++++++- 3 files changed, 106 insertions(+), 22 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index 212e320..c271d9c 100644 --- a/main.c +++ b/main.c @@ -3,6 +3,9 @@ /* Parsed assembly */ static AsmLine *allasm = NULL; +/* Number of assembly the relaxation passes. */ +static int nrelax = 1; + /* Symbol table. */ static struct hashtable *symbols = NULL; @@ -44,8 +47,11 @@ static Symbol *getsym(const char *name) { htabkey(&htk, name, strlen(name)); ps = (Symbol **)htabput(symbols, &htk); if (!*ps) { - *ps = zalloc(sizeof(Symbol)); - (*ps)->name = name; + *ps = xmalloc(sizeof(Symbol)); + **ps = (Symbol){ + .name = name, + .wco = -1, + }; } s = *ps; return s; @@ -377,7 +383,7 @@ static void assemblemem(const Memarg *memarg, Rex rex, VarBytes prefix, assemblemodregrm(rex, prefix, opcode, mod, reg, rm); if (mod == 1) { - assemblereloc(memarg->disp.l, memarg->disp.c, 1, R_X86_64_8); + assembleconstant(memarg->disp.c, 1); } else if (mod == 2) { assemblereloc(memarg->disp.l, memarg->disp.c, 4, R_X86_64_32); } @@ -437,7 +443,7 @@ static void assemblemem(const Memarg *memarg, Rex rex, VarBytes prefix, sb(sibbyte(scale, index, base)); if (mod == 1) { - assemblereloc(memarg->disp.l, memarg->disp.c, 1, R_X86_64_8); + assembleconstant(memarg->disp.c, 1); } else if (mod == 2) { assemblereloc(memarg->disp.l, memarg->disp.c, 4, R_X86_64_32); } @@ -777,6 +783,43 @@ static void assembleimul(const Instr *instr) { } } +static void assemblejmp(const Jmp *j) { + int jmpsize; + int64_t distance; + Symbol *target; + + static uint8_t variant2op[31] = { + 0xe9, 0x84, 0x88, 0x8b, 0x8a, 0x8a, 0x80, 0x85, 0x89, 0x8b, 0x81, + 0x8f, 0x8d, 0x8c, 0x8e, 0x85, 0x83, 0x87, 0x83, 0x82, 0x86, 0x8e, + 0x8c, 0x8d, 0x8f, 0x84, 0x82, 0x86, 0x82, 0x83, 0x87, + }; + + jmpsize = 4; + target = getsym(j->target); + if (cursection == target->section && (target->defined || target->wco != -1)) { + if (target->defined) { + distance = target->offset - cursection->hdr.sh_size; + } else { + distance = target->wco - cursection->hdr.sh_size; + } + if ((distance - 1) >= -128 && (distance - 1) <= 127) { + jmpsize = 1; + } else { + jmpsize = 4; + } + } + + if (jmpsize == 4) { + if (j->variant) + sb(0x0f); + sb(variant2op[j->variant]); + assemblereloc(j->target, -4, 4, R_X86_64_PC32); + } else { + sb(variant2op[j->variant] + (j->variant ? -16 : 2)); + assemblereloc(j->target, -1, 1, R_X86_64_PC8); + } +} + static void assemble(void) { Symbol *sym; AsmLine *l; @@ -886,18 +929,9 @@ static void assemble(void) { case ASM_CALL: assemblecall(&v->call); break; - case ASM_JMP: { - static uint8_t variant2op[31] = { - 0xe9, 0x84, 0x88, 0x8b, 0x8a, 0x8a, 0x80, 0x85, 0x89, 0x8b, 0x81, - 0x8f, 0x8d, 0x8c, 0x8e, 0x85, 0x83, 0x87, 0x83, 0x82, 0x86, 0x8e, - 0x8c, 0x8d, 0x8f, 0x84, 0x82, 0x86, 0x82, 0x83, 0x87, - }; - if (v->jmp.variant) - sb(0x0f); - sb(variant2op[v->jmp.variant]); - assemblereloc(v->jmp.target, -4, 4, R_X86_64_PC32); + case ASM_JMP: + assemblejmp(&v->jmp); break; - } case ASM_PUSH: { Rex rex; uint8_t reg; @@ -1088,10 +1122,9 @@ static void assemble(void) { assemblebasicop(&v->instr, variant2op[v->instr.variant], 0x01); break; } - case ASM_PXOR: { + case ASM_PXOR: assemblerrm(&v->instr, 0x66, 0x01000fef, 1); break; - } case ASM_SET: assembleset(&v->instr); break; @@ -1155,6 +1188,32 @@ static void assemble(void) { } } +/* Reset while remembering symbol offsets so we can size jumps. */ +static void relaxreset(void) { + Symbol *sym; + Section *sec; + size_t i; + + /* Reset relocations and section data but retain capacity. */ + nrelocs = 0; + + for (i = 0; i < nsections; i++) { + sec = §ions[i]; + if (sec == shstrtab) + continue; + sec->hdr.sh_size = 0; + } + + /* Reset symbols, saving the worst case offset for the second pass. */ + for (i = 0; i < symbols->cap; i++) { + if (!symbols->keys[i].str) + continue; + sym = symbols->vals[i]; + *sym = (Symbol){ + .name = sym->name, .section = sym->section, .wco = sym->offset}; + } +} + static void addtosymtab(Symbol *sym) { Elf64_Sym elfsym; int stype; @@ -1219,10 +1278,14 @@ static int resolvereloc(Relocation *reloc) { return 0; switch (reloc->type) { - case R_X86_64_8: case R_X86_64_32: case R_X86_64_64: return 0; + case R_X86_64_PC8: + rdata = &reloc->section->data[reloc->offset]; + value = sym->offset - reloc->offset + reloc->addend; + rdata[0] = ((uint8_t)value & 0xff); + return 1; case R_X86_64_PC32: rdata = &reloc->section->data[reloc->offset]; value = sym->offset - reloc->offset + reloc->addend; @@ -1258,7 +1321,6 @@ static void appendreloc(Relocation *reloc) { case R_X86_64_PC32: case R_X86_64_32: case R_X86_64_64: - case R_X86_64_8: elfrel.r_info = ELF64_R_INFO(sym->idx, reloc->type); elfrel.r_offset = reloc->offset; elfrel.r_addend = reloc->addend; @@ -1328,8 +1390,11 @@ static void outelf(void) { } static void usage(char *argv0) { - fprintf(stderr, "minias - a mini assembler."); - fprintf(stderr, "usage: %s [-o out] [input]\n", argv0); + fprintf(stderr, "minias - a mini x86-64 assembler.\n\n"); + fprintf(stderr, "usage: %s [-r passes] [-o out] [input]\n", argv0); + fprintf(stderr, "\n"); + fprintf(stderr, " -r passes Jump relaxation iterations (default 1).\n"); + fprintf(stderr, " -o out Output file to write (default stdout).\n"); exit(2); } @@ -1347,6 +1412,9 @@ static void parseargs(int argc, char *argv[]) { case 'h': usage(argv0); break; + case 'r': + nrelax = atoi(*++argv); + break; case 'o': if (argv[1] == NULL) usage(argv0); @@ -1375,6 +1443,10 @@ int main(int argc, char *argv[]) { allasm = parseasm(); initsections(); assemble(); + while (nrelax-- > 0) { + relaxreset(); + assemble(); + } fillsymtab(); handlerelocs(); outelf(); diff --git a/minias.h b/minias.h index 020de41..5914c1b 100644 --- a/minias.h +++ b/minias.h @@ -24,6 +24,7 @@ typedef struct { const char *name; int32_t idx; int64_t offset; + int64_t wco; /* worst case offset */ int64_t size; int global; int defined; diff --git a/test/test.sh b/test/test.sh index f1015a4..7b0e41c 100644 --- a/test/test.sh +++ b/test/test.sh @@ -8,7 +8,7 @@ tmpb="$(mktemp)" trap "rm -f \"$tmps\" \"$tmpo\" \"$tmpb\"" EXIT t () { - echo "$1" > "$tmps" + echo -e "$1" > "$tmps" clang -Wno-everything -c -x assembler "$tmps" -o "$tmpo" objcopy -j ".text" -O binary "$tmpo" "$tmpb" want="$(xxd -ps "$tmpb" | head -n 1 | cut -d ' ' -f 2-)" @@ -152,6 +152,17 @@ conditioncodes=" o p pe po s z " +for fill in 0 1 129 +do + t "l:\n .fill $fill, 1, 0x00 \njmp l" + t "jmp l\n .fill $fill, 1, 0x00 \nl:" + for cc in $conditioncodes + do + t "l:\n .fill $fill, 1, 0x00 \nj${cc} l" + t "j${cc} l\n .fill $fill, 1, 0x00 \nl:" + done +done + for cc in $conditioncodes do t "set${cc} %al" -- cgit v1.2.3