aboutsummaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorAndrew Chambers <[email protected]>2021-10-09 18:22:43 +1300
committerAndrew Chambers <[email protected]>2021-10-09 18:22:43 +1300
commita439d4004379cc73dd6b2bf330130733396f9d61 (patch)
treeb62315e59ea9f7dfaab7156ffa1ffab8ab0855e3 /main.c
parentf72806dec51ef5ffcf2883566b070c8508ebf30a (diff)
More tests.
Diffstat (limited to 'main.c')
-rw-r--r--main.c41
1 files changed, 25 insertions, 16 deletions
diff --git a/main.c b/main.c
index 9b7f31f..5914994 100644
--- a/main.c
+++ b/main.c
@@ -307,20 +307,7 @@ static void assembleplusr(uint8_t opcode, uint8_t rexw, AsmKind reg) {
sb(opcode | (bits & 7));
}
-/* Assemble a symbolic value. */
-static void assemblereloc(const char *l, int64_t c, int nbytes, int type) {
- Relocation *reloc;
- Symbol *sym;
-
- if (l != NULL) {
- reloc = newreloc();
- sym = getsym(l);
- reloc->type = type;
- reloc->section = cursection;
- reloc->sym = sym;
- reloc->offset = cursection->hdr.sh_size;
- }
-
+void assembleconstant(int64_t c, int nbytes) {
switch (nbytes) {
case 1:
sb((uint8_t)c);
@@ -332,24 +319,46 @@ static void assemblereloc(const char *l, int64_t c, int nbytes, int type) {
su32((uint32_t)c);
break;
case 8:
- fatal("TODO 8 byte symbolic");
+ fatal("TODO 8 byte");
break;
default:
unreachable();
}
}
+/* Assemble a symbolic value. */
+static void assemblereloc(const char *l, int64_t c, int nbytes, int type) {
+ Relocation *reloc;
+ Symbol *sym;
+
+ if (l != NULL) {
+ reloc = newreloc();
+ sym = getsym(l);
+ reloc->type = type;
+ reloc->section = cursection;
+ reloc->sym = sym;
+ reloc->offset = cursection->hdr.sh_size;
+ }
+ assembleconstant(c, nbytes);
+}
+
/* Assemble a r <-> (%rip) operation. */
static void assembleriprel(Memarg *memarg, uint8_t rexw, uint8_t opcode,
uint8_t reg, uint8_t opsz) {
uint8_t rex;
+
if (opsz == 2)
sb(0x66);
rex = rexbyte(rexw, 0, 0, 0);
if (rex != rexbyte(0, 0, 0, 0))
sb(rex);
sb2(opcode, modregrm(0x00, reg, 0x05));
- assemblereloc(memarg->l, memarg->c - 4, 4, R_X86_64_PC32);
+
+ if (memarg->l) {
+ assemblereloc(memarg->l, memarg->c - 4, 4, R_X86_64_PC32);
+ } else {
+ assembleconstant(memarg->c, 4);
+ }
}
/* Assemble a r <-> mem operation. */