1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
|
#include "minias.h"
/* Parsed assembly */
static AsmLine *allasm = NULL;
/* Number of assembly the relaxation passes. */
static int nrelax = 1;
/* Symbol table. */
static struct hashtable *symbols = NULL;
/* Array of all relocations before adding to the rel section. */
static Relocation *relocs = NULL;
static size_t nrelocs = 0;
static size_t reloccap = 0;
#define MAXSECTIONS 32
static Section sections[MAXSECTIONS];
static size_t nsections = 1; // first is reserved.
static Section *cursection = NULL;
static Section *shstrtab = NULL;
static Section *strtab = NULL;
static Section *symtab = NULL;
static Section *bss = NULL;
static Section *text = NULL;
static Section *data = NULL;
static Section *textrel = NULL;
static Section *datarel = NULL;
static char *infilename = "<stdin>";
static size_t curlineno = 0;
static void lfatal(const char *fmt, ...) {
va_list ap;
fprintf(stderr, "%s:%ld: ", infilename, curlineno);
va_start(ap, fmt);
vwarn(fmt, ap);
va_end(ap);
exit(1);
}
static Symbol *getsym(const char *name) {
Symbol **ps, *s;
struct hashtablekey htk;
htabkey(&htk, name, strlen(name));
ps = (Symbol **)htabput(symbols, &htk);
if (!*ps) {
*ps = xmalloc(sizeof(Symbol));
**ps = (Symbol){
.name = name,
.wco = -1,
};
}
s = *ps;
return s;
}
static void secaddbytes(Section *s, const void *bytes, size_t n) {
if (s->hdr.sh_type == SHT_NOBITS) {
s->hdr.sh_size += n;
return;
}
while (s->capacity < s->hdr.sh_size + n) {
s->capacity = s->capacity ? (s->capacity * 2) : 512;
s->data = xrealloc(s->data, s->capacity);
}
memcpy(s->data + s->hdr.sh_size, bytes, n);
s->hdr.sh_size += n;
}
static void secaddbyte(Section *s, uint8_t b) { secaddbytes(s, &b, 1); }
static Elf64_Word elfstr(Section *sec, const char *s) {
Elf64_Word i = sec->hdr.sh_size;
secaddbytes(sec, s, strlen(s) + 1);
return i;
}
static Section *newsection() {
Section *s;
if (nsections >= MAXSECTIONS)
fatal("too many sections");
s = §ions[nsections];
s->idx = nsections;
nsections += 1;
return s;
}
static Section *getsection(const char *name) {
size_t i;
char *secname;
Section *s;
for (i = 0; i < nsections; i++) {
secname = (char *)shstrtab->data + sections[i].hdr.sh_name;
if (strcmp(secname, name) == 0)
return §ions[i];
}
s = newsection();
s->hdr.sh_name = elfstr(shstrtab, name);
return s;
}
static void initsections(void) {
Elf64_Sym elfsym;
shstrtab = newsection();
secaddbyte(shstrtab, 0);
shstrtab->hdr.sh_name = elfstr(shstrtab, ".shstrtab");
shstrtab->hdr.sh_type = SHT_STRTAB;
strtab = newsection();
secaddbyte(strtab, 0);
strtab->hdr.sh_name = elfstr(shstrtab, ".strtab");
strtab->hdr.sh_type = SHT_STRTAB;
symtab = newsection();
symtab->hdr.sh_name = elfstr(shstrtab, ".symtab");
symtab->hdr.sh_type = SHT_SYMTAB;
symtab->hdr.sh_link = strtab->idx;
symtab->hdr.sh_entsize = sizeof(Elf64_Sym);
memset(&elfsym, 0, sizeof(elfsym));
secaddbytes(symtab, &elfsym, sizeof(Elf64_Sym));
bss = newsection();
bss->hdr.sh_name = elfstr(shstrtab, ".bss");
bss->hdr.sh_type = SHT_NOBITS;
bss->hdr.sh_flags = SHF_ALLOC | SHF_WRITE;
bss->hdr.sh_addralign = 16; // XXX right value?
data = newsection();
data->hdr.sh_name = elfstr(shstrtab, ".data");
data->hdr.sh_type = SHT_PROGBITS;
data->hdr.sh_flags = SHF_ALLOC | SHF_WRITE;
data->hdr.sh_addralign = 16; // XXX right value?
text = newsection();
text->hdr.sh_name = elfstr(shstrtab, ".text");
text->hdr.sh_type = SHT_PROGBITS;
text->hdr.sh_flags = SHF_EXECINSTR | SHF_ALLOC;
text->hdr.sh_addralign = 4;
textrel = newsection();
textrel->hdr.sh_name = elfstr(shstrtab, ".rela.text");
textrel->hdr.sh_type = SHT_RELA;
textrel->hdr.sh_info = text->idx;
textrel->hdr.sh_link = symtab->idx;
textrel->hdr.sh_entsize = sizeof(Elf64_Rela);
datarel = newsection();
datarel->hdr.sh_name = elfstr(shstrtab, ".rela.data");
datarel->hdr.sh_type = SHT_RELA;
datarel->hdr.sh_info = data->idx;
datarel->hdr.sh_link = symtab->idx;
datarel->hdr.sh_entsize = sizeof(Elf64_Rela);
}
static Relocation *newreloc() {
if (nrelocs == reloccap) {
reloccap = nrelocs ? nrelocs * 2 : 64;
relocs = xreallocarray(relocs, reloccap, sizeof(Relocation));
}
return &relocs[nrelocs++];
}
/* Shorthand helpers to write section data. */
static void sb(uint8_t b) { secaddbyte(cursection, b); }
static void sb2(uint8_t b1, uint8_t b2) {
uint8_t buf[2] = {b1, b2};
secaddbytes(cursection, buf, sizeof(buf));
}
static void sbn(uint8_t *bytes, size_t n) { secaddbytes(cursection, bytes, n); }
static void su16(uint16_t w) {
uint8_t buf[2] = {w & 0xff, (w & 0xff00) >> 8};
secaddbytes(cursection, buf, sizeof(buf));
}
static void su32(uint32_t l) {
uint8_t buf[4] = {
l & 0xff,
(l & 0xff00) >> 8,
(l & 0xff0000) >> 16,
(l & 0xff000000) >> 24,
};
secaddbytes(cursection, buf, sizeof(buf));
}
static void su64(uint64_t l) {
uint8_t buf[8] = {
l & 0xff,
(l & 0xff00) >> 8,
(l & 0xff0000) >> 16,
(l & 0xff000000) >> 24,
(l & 0xff00000000) >> 32,
(l & 0xff0000000000) >> 40,
(l & 0xff000000000000) >> 48,
(l & 0xff00000000000000) >> 56,
};
secaddbytes(cursection, buf, sizeof(buf));
}
/* Convert an AsmKind to register bits in reg/rm style. */
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; }
/* Rex opcode prefix. */
typedef struct Rex {
uint8_t required : 1;
uint8_t w : 1;
uint8_t r : 1;
uint8_t x : 1;
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);
}
/* Compose a mod/reg/rm byte - See intel manual. */
static uint8_t modregrmbyte(uint8_t mod, uint8_t reg, uint8_t rm) {
return (((mod & 3) << 6) | ((reg & 7) << 3) | (rm & 7));
}
/* Compose an sib byte - See intel manual. */
static uint8_t sibbyte(uint8_t ss, uint8_t idx, uint8_t base) {
return (((ss & 3) << 6) | ((idx & 7) << 3) | (base & 7));
}
void assembleconstant(int64_t c, int nbytes) {
switch (nbytes) {
case 1:
sb((uint8_t)c);
break;
case 2:
su16((uint16_t)c);
break;
case 4:
su32((uint32_t)c);
break;
case 8:
su64((uint64_t)c);
break;
default:
unreachable();
}
}
/* The VarBytes type encodes a variadic number of bytes.
The top byte is how many bytes we encode less 1.
examples:
<nothing> encodes as -1
0c encodes as 0x0000000c
02 03 encodes as 0x01000203
*/
typedef int32_t VarBytes;
static void assemblevbytes(VarBytes bytes) {
int i, n;
uint8_t b, shift;
n = (int8_t)(uint8_t)((bytes & 0xff000000) >> 24);
for (i = n; i >= 0; i--) {
shift = i * 8;
b = (bytes & (0xff << shift)) >> shift;
sb(b);
}
}
static void assemblerex(Rex rex) {
if (rex.required || rex.w || rex.r || rex.x || rex.b)
sb(rexbyte(rex));
}
/* Assemble op +rw | op + rd. */
static void assembleplusr(Rex rex, VarBytes prefix, VarBytes opcode,
uint8_t reg) {
assemblevbytes(prefix);
assemblerex(rex);
assemblevbytes(opcode | (reg & 7));
}
static void assemblemodregrm(Rex rex, VarBytes prefix, VarBytes opcode,
uint8_t mod, uint8_t reg, uint8_t rm) {
assemblevbytes(prefix);
assemblerex(rex);
assemblevbytes(opcode);
sb(modregrmbyte(mod, reg, rm));
}
/* 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;
reloc->addend = c;
c = 0;
}
assembleconstant(c, nbytes);
}
/* Rip relative addressing. */
static void assembleriprel(const Memarg *memarg, Rex rex, VarBytes prefix,
VarBytes opcode, uint8_t reg, int32_t adjust) {
uint8_t rm;
rm = 0x05;
assemblemodregrm(rex, prefix, opcode, 0x00, reg, rm);
if (memarg->disp.l) {
assemblereloc(memarg->disp.l, memarg->disp.c - 4 + adjust, 4,
R_X86_64_PC32);
} else {
assembleconstant(memarg->disp.c, 4);
}
}
/* Assemble a r <-> mem operation. */
static void assemblemem(const Memarg *memarg, Rex rex, VarBytes prefix,
VarBytes opcode, uint8_t reg) {
uint8_t mod, rm, scale, index, base;
if (memarg->base == ASM_RIP) {
assembleriprel(memarg, rex, prefix, opcode, reg, 0);
return;
}
/* Direct memory access */
if (memarg->base == ASM_NO_REG) {
mod = 0;
rm = 4;
assemblemodregrm(rex, prefix, opcode, mod, reg, rm);
sb(sibbyte(0, 4, 5));
if (memarg->disp.l) {
assemblereloc(memarg->disp.l, memarg->disp.c, 4, R_X86_64_32);
} else {
assembleconstant(memarg->disp.c, 4);
}
return;
}
rm = regbits(memarg->base);
rex.b = !!(rm & (1 << 3));
/* Case when we don't need sib */
if (memarg->index == ASM_NO_REG && memarg->scale == 0 && ((rm & 7) != 4)) {
if (memarg->disp.l == 0 && memarg->disp.c == 0) {
if ((rm & 7) == 5) {
mod = 1;
} else {
mod = 0;
}
} else {
mod = 2;
}
assemblemodregrm(rex, prefix, opcode, mod, reg, rm);
if (mod == 1) {
assembleconstant(memarg->disp.c, 1);
} else if (mod == 2) {
assemblereloc(memarg->disp.l, memarg->disp.c, 4, R_X86_64_32);
}
return;
}
/* Setup sib indexing. */
base = rm;
rm = 4;
if (memarg->disp.c == 0 && memarg->disp.l == 0 && ((base & 7) != 5)) {
mod = 0; /* +0 */
} else {
if (memarg->disp.l == NULL && memarg->disp.c >= -128 &&
memarg->disp.c <= 127) {
mod = 1; /* +disp8 */
} else {
mod = 2; /* +disp32 */
}
}
if (memarg->index == ASM_NO_REG) {
index = 4;
} else {
if (memarg->index == ASM_RSP)
lfatal("rsp cannot be used as an index");
index = regbits(memarg->index);
}
/* If our base is a bp register, we must use the index instead. */
if ((base & 7) == 5 && memarg->index == ASM_NO_REG) {
index = base;
}
rex.x = !!(index & (1 << 3));
switch (memarg->scale) {
case 0:
case 1:
scale = 0;
break;
case 2:
scale = 1;
break;
case 4:
scale = 2;
break;
case 8:
scale = 3;
break;
default:
lfatal("invalid addressing scale");
return;
}
assemblemodregrm(rex, prefix, opcode, mod, reg, rm);
sb(sibbyte(scale, index, base));
if (mod == 1) {
assembleconstant(memarg->disp.c, 1);
} else if (mod == 2) {
assemblereloc(memarg->disp.l, memarg->disp.c, 4, R_X86_64_32);
}
}
/* Assemble op + imm -> r/m. */
static void assembleimmrm(const Instr *instr, Rex rex, VarBytes prefix,
VarBytes opcode, uint8_t immreg) {
uint8_t rm;
const Memarg *memarg;
const Imm *imm;
imm = &instr->arg1->imm;
if (instr->arg2->kind == ASM_MEMARG) {
memarg = &instr->arg2->memarg;
if (memarg->base == ASM_RIP) {
assembleriprel(memarg, rex, prefix, opcode, immreg, -imm->nbytes);
} else {
assemblemem(memarg, rex, prefix, opcode, immreg);
}
} else {
rm = regbits(instr->arg2->kind);
rex.required = isrexreg(instr->arg2->kind);
rex.b = !!(rm & (1 << 3));
assemblemodregrm(rex, prefix, opcode, 0x03, immreg, rm);
}
assemblereloc(imm->v.l, imm->v.c, imm->nbytes, R_X86_64_32);
}
/* Assemble op + r <-> r/m. */
static void assemblerrm(const Instr *instr, VarBytes prefix, VarBytes opcode,
int invert) {
Rex rex;
AsmKind regarg;
uint8_t reg1bits, reg2bits;
const Memarg *memarg;
const Parsev *arg1, *arg2;
if (invert) {
arg1 = instr->arg2;
arg2 = instr->arg1;
} else {
arg1 = instr->arg1;
arg2 = instr->arg2;
}
if (arg1->kind == ASM_MEMARG) {
memarg = &arg1->memarg;
regarg = arg2->kind;
reg1bits = regbits(regarg);
rex = (Rex){.required = isrexreg(regarg),
.w = isreg64(regarg),
.r = !!(reg1bits & (1 << 3))};
assemblemem(memarg, rex, prefix, opcode, reg1bits);
} else if (arg2->kind == ASM_MEMARG) {
memarg = &arg2->memarg;
regarg = arg1->kind;
reg1bits = regbits(regarg);
rex = (Rex){.required = isrexreg(regarg),
.w = isreg64(regarg),
.r = !!(reg1bits & (1 << 3))};
assemblemem(memarg, rex, prefix, opcode, reg1bits);
} else {
reg1bits = regbits(arg1->kind);
reg2bits = regbits(arg2->kind);
rex = (Rex){.required = isrexreg(arg1->kind) || isrexreg(arg2->kind),
.w = isreg64(arg1->kind) || isreg64(arg2->kind),
.r = !!(reg1bits & (1 << 3)),
.b = !!(reg2bits & (1 << 3))};
assemblemodregrm(rex, prefix, opcode, 0x03, reg1bits, reg2bits);
}
}
/* Assemble a 'basic op' which is just a repeated op pattern we have named. */
static void assemblebasicop(const Instr *instr, VarBytes opcode,
uint8_t immreg) {
VarBytes prefix;
const Imm *imm;
Rex rex;
prefix = ((instr->variant % 4) == 1) ? 0x66 : -1;
if (instr->variant < 4) {
imm = &instr->arg1->imm;
rex = (Rex){
.w = instr->variant == 3,
};
assemblevbytes(prefix);
assemblerex(rex);
assemblevbytes(opcode);
assemblereloc(imm->v.l, imm->v.c, imm->nbytes, R_X86_64_32);
} else if (instr->variant < 12) {
rex = (Rex){
.w = (instr->variant % 4) == 3,
};
assembleimmrm(instr, rex, prefix, opcode, immreg);
} else {
assemblerrm(instr, prefix, opcode, 0);
}
}
static void assemblexchg(const Instr *xchg) {
Rex rex;
AsmKind reg;
VarBytes prefix, opcode;
static uint8_t variant2op[18] = {0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
0x86, 0x87, 0x87, 0x87, 0x86, 0x87,
0x87, 0x87, 0x86, 0x87, 0x87, 0x87};
opcode = variant2op[xchg->variant];
if (xchg->variant < 6) {
reg = (xchg->variant % 2) ? xchg->arg1->kind : xchg->arg2->kind;
prefix = (xchg->variant < 2) ? 0x66 : -1;
rex = (Rex){
.required = isrexreg(xchg->arg1->kind) || isrexreg(xchg->arg2->kind),
.w = isreg64(xchg->arg1->kind) || isreg64(xchg->arg2->kind),
.b = !!(regbits(reg) & (1 << 3)),
};
assembleplusr(rex, prefix, opcode, regbits(reg));
} else {
prefix = (((xchg->variant - 6) % 4) == 1) ? 0x66 : -1;
assemblerrm(xchg, prefix, opcode, 0);
}
}
static void assemblemov(const Instr *mov) {
Rex rex;
int8_t reg;
const Imm *imm;
VarBytes prefix, opcode;
static uint8_t variant2op[20] = {0x88, 0x89, 0x89, 0x89, 0x88, 0x89, 0x89,
0x89, 0x8a, 0x8b, 0x8b, 0x8b, 0xc6, 0xc7,
0xc7, 0xc7, 0xb0, 0xb8, 0xb8, 0xc7};
prefix = ((mov->variant % 4) == 1) ? 0x66 : -1;
opcode = variant2op[mov->variant];
if (mov->variant < 12) {
assemblerrm(mov, prefix, opcode, 0);
} else if (mov->variant < 16) {
rex = (Rex){.w = (mov->variant % 4) == 3};
assembleimmrm(mov, rex, prefix, opcode, 0x00);
} else if (mov->variant == 19) {
uint64_t mask, maskedc;
imm = &mov->arg1->imm;
mask = 0xffffffff80000000;
maskedc = ((uint64_t)imm->v.c) & mask;
if ((maskedc == mask || maskedc == 0) && imm->v.l == NULL) {
/* Sign extension works for this value, regular mov */
reg = regbits(mov->arg2->kind);
rex = (Rex){
.required = isrexreg(mov->arg2->kind),
.w = 1,
.b = !!(reg & (1 << 3)),
};
assemblemodregrm(rex, prefix, opcode, 0x03, 0x00, reg);
assemblereloc(imm->v.l, imm->v.c, 4, R_X86_64_32);
} else {
/* 64 bit immediate required. */
reg = regbits(mov->arg2->kind);
rex = (Rex){
.required = isrexreg(mov->arg2->kind),
.w = 1,
.b = !!(reg & (1 << 3)),
};
assemblerex(rex);
sb(0xb8 | (reg & 7));
assemblereloc(imm->v.l, imm->v.c, 8, R_X86_64_64);
}
} else {
imm = &mov->arg1->imm;
reg = regbits(mov->arg2->kind);
rex = (Rex){
.required = isrexreg(mov->arg2->kind),
.w = isreg64(mov->arg2->kind),
.b = !!(reg & (1 << 3)),
};
assembleplusr(rex, prefix, opcode, reg);
assemblereloc(imm->v.l, imm->v.c, imm->nbytes, R_X86_64_32);
}
}
static void assemblemovextend(const Instr *mov, VarBytes opcode) {
VarBytes prefix;
if (mov->variant == 0 || mov->variant == 5)
prefix = 0x66;
else
prefix = -1;
assemblerrm(mov, prefix, opcode, 1);
}
static void assembledivmulneg(const Instr *instr, uint8_t reg) {
Rex rex;
VarBytes prefix, opcode;
uint8_t rm;
rex = (Rex){
.w = (instr->variant % 4) == 3,
};
prefix = (instr->variant % 4) == 1 ? 0x66 : -1;
opcode = (instr->variant % 4) == 1 ? 0xf6 : 0xf7;
if (instr->arg1->kind == ASM_MEMARG) {
assemblemem(&instr->arg1->memarg, rex, prefix, opcode, reg);
} else {
rm = regbits(instr->arg1->kind);
rex.w = isreg64(instr->arg1->kind);
rex.r = !!(reg & (1 << 3));
rex.b = !!(rm & (1 << 3));
assemblemodregrm(rex, prefix, opcode, 0x03, reg, rm);
}
}
static void assembleshift(const Instr *instr, uint8_t immreg) {
Rex rex;
VarBytes prefix, opcode;
uint8_t rm;
opcode = (instr->variant < 6) ? 0xd3 : 0xc1;
rex = (Rex){.w = (instr->variant % 3) == 2};
prefix = ((instr->variant % 3) == 0) ? 0x66 : -1;
if (instr->arg1->kind == ASM_IMM) {
assembleimmrm(instr, rex, prefix, opcode, immreg);
} else if (instr->arg2->kind == ASM_MEMARG) {
assemblemem(&instr->arg2->memarg, rex, prefix, opcode, immreg);
} else {
rm = regbits(instr->arg2->kind);
rex.r = !!(immreg & (1 << 3));
rex.b = !!(rm & (1 << 3));
assemblemodregrm(rex, prefix, opcode, 0x03, immreg, rm);
}
}
static void assemblemovsmmx(const Instr *instr, VarBytes prefix) {
VarBytes opcode;
if (instr->variant == 2) {
opcode = 0x01000f11;
assemblerrm(instr, prefix, opcode, 0);
} else {
opcode = 0x01000f10;
assemblerrm(instr, prefix, opcode, 1);
}
}
static void assembletest(const Instr *instr) {
const Imm *imm;
Rex rex;
VarBytes prefix;
uint8_t byteop;
byteop = ((instr->variant % 4) == 0);
prefix = ((instr->variant % 4) == 1) ? 0x66 : -1;
if (instr->variant < 4) {
rex = (Rex){.w = instr->variant == 3};
assemblevbytes(prefix);
assemblerex(rex);
assemblevbytes(byteop ? 0xa8 : 0xa9);
imm = &instr->arg1->imm;
assemblereloc(imm->v.l, imm->v.c, imm->nbytes, R_X86_64_32);
} else if (instr->variant < 12) {
rex = (Rex){.w = (instr->variant % 4) == 3};
assembleimmrm(instr, rex, prefix, byteop ? 0xf6 : 0xf7, 0);
} else {
assemblerrm(instr, prefix, byteop ? 0x84 : 0x85, 0);
}
}
static void assembleset(const Instr *instr) {
Rex rex;
VarBytes prefix, opcode;
uint8_t reg, rm;
static uint8_t variant2op[30] = {
0x94, 0x98, 0x9b, 0x9a, 0x9a, 0x90, 0x95, 0x99, 0x9b, 0x91,
0x9f, 0x9d, 0x9c, 0x9e, 0x95, 0x93, 0x97, 0x93, 0x92, 0x96,
0x9e, 0x9c, 0x9d, 0x9f, 0x94, 0x92, 0x96, 0x92, 0x93, 0x97,
};
opcode = 0x01000f00 | variant2op[instr->variant % 31];
prefix = -1;
if (instr->arg1->kind == ASM_MEMARG) {
rex = (Rex){0};
assemblemem(&instr->arg1->memarg, rex, prefix, opcode, 0);
} else {
rm = regbits(instr->arg1->kind);
rex = (Rex){
.required = isrexreg(instr->arg1->kind),
.w = isreg64(instr->arg1->kind),
.b = !!(rm & (1 << 3)),
};
assemblemodregrm(rex, prefix, opcode, 0x03, 0, rm);
}
}
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 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;
const Parsev *v;
cursection = text;
curlineno = 0;
for (l = allasm; l; l = l->next) {
curlineno++;
v = l->v;
switch (v->kind) {
case ASM_SYNTAX_ERROR:
lfatal("syntax error");
break;
case ASM_BLANK:
break;
case ASM_DIR_GLOBL:
sym = getsym(v->globl.name);
sym->global = 1;
break;
case ASM_DIR_SECTION: {
const char *fp;
Section *s;
s = getsection(v->section.name);
s->hdr.sh_type = v->section.type;
fp = v->section.flags;
while (fp && *fp) {
switch (*(fp++)) {
case 'a':
s->hdr.sh_flags |= SHF_ALLOC;
break;
case 'w':
s->hdr.sh_flags |= SHF_WRITE;
break;
case 'x':
s->hdr.sh_flags |= SHF_EXECINSTR;
break;
default:
unreachable();
}
}
cursection = s;
break;
}
case ASM_DIR_DATA:
cursection = data;
break;
case ASM_DIR_TEXT:
cursection = text;
break;
case ASM_DIR_ASCII:
sbn(v->ascii.data, v->ascii.len);
break;
case ASM_DIR_ASCIIZ:
sbn(v->asciiz.data, v->asciiz.len);
sb(0x00);
break;
case ASM_DIR_BALIGN: {
int64_t i, rem, amnt;
amnt = 0;
rem = cursection->hdr.sh_size % v->balign.align;
if (rem)
amnt = v->balign.align - rem;
for (i = 0; i < amnt; i++) {
sb(0x00);
}
break;
}
case ASM_DIR_FILL: {
ssize_t i = 0;
for (i = 0; i < v->fill.repeat; i++) {
switch (v->fill.size) {
case 1:
case 2:
case 4:
case 8:
assembleconstant(v->fill.value, v->fill.size);
break;
default:
lfatal("unsupported fill size '%d'", v->fill.size);
}
}
break;
}
case ASM_DIR_BYTE:
assemblereloc(v->dirbyte.value.l, v->dirbyte.value.c, 1, R_X86_64_32);
break;
case ASM_DIR_SHORT:
assemblereloc(v->dirshort.value.l, v->dirshort.value.c, 2, R_X86_64_32);
break;
case ASM_DIR_INT:
assemblereloc(v->dirint.value.l, v->dirint.value.c, 4, R_X86_64_32);
break;
case ASM_DIR_QUAD:
assemblereloc(v->dirquad.value.l, v->dirquad.value.c, 8, R_X86_64_64);
break;
case ASM_LABEL:
sym = getsym(v->label.name);
sym->section = cursection;
sym->offset = cursection->hdr.sh_size;
if (sym->defined)
lfatal("%s already defined", sym->name);
sym->defined = 1;
break;
case ASM_CALL:
assemblecall(&v->call);
break;
case ASM_JMP:
assemblejmp(&v->jmp);
break;
case ASM_PUSH: {
Rex rex;
uint8_t reg;
if (v->instr.arg1->kind == ASM_MEMARG) {
rex = (Rex){0};
assemblemem(&v->instr.arg1->memarg, rex, -1, 0xff, 0x06);
} else {
reg = regbits(v->instr.arg1->kind);
rex = (Rex){.b = !!(reg & (1 << 3))};
assembleplusr(rex, -1, 0x50, reg);
}
break;
}
case ASM_POP: {
Rex rex;
uint8_t reg;
if (v->instr.arg1->kind == ASM_MEMARG) {
rex = (Rex){0};
assemblemem(&v->instr.arg1->memarg, rex, -1, 0x8f, 0x00);
} else {
reg = regbits(v->instr.arg1->kind);
rex = (Rex){.b = !!(reg & (1 << 3))};
assembleplusr(rex, -1, 0x58, reg);
}
break;
}
case ASM_NOP:
sb(0x90);
break;
case ASM_LEAVE:
sb(0xc9);
break;
case ASM_CLTD:
sb(0x99);
break;
case ASM_CQTO:
sb2(0x48, 0x99);
break;
case ASM_CVTSI2SD:
assemblerrm(&v->instr, 0xf2, 0x01000f2a, 1);
break;
case ASM_CVTSI2SS:
assemblerrm(&v->instr, 0xf3, 0x01000f2a, 1);
break;
case ASM_CVTSS2SD:
assemblerrm(&v->instr, 0xf3, 0x01000f5a, 1);
break;
case ASM_CVTSD2SS:
assemblerrm(&v->instr, 0xf2, 0x01000f5a, 1);
break;
case ASM_CVTTSD2SI:
assemblerrm(&v->instr, 0xf2, 0x01000f2c, 1);
break;
case ASM_CVTTSS2SI:
assemblerrm(&v->instr, 0xf3, 0x01000f2c, 1);
break;
case ASM_RET:
sb(0xc3);
break;
case ASM_LEA: {
VarBytes prefix;
prefix = v->instr.variant == 0 ? 0x66 : -1;
assemblerrm(&v->instr, prefix, 0x8d, 0);
break;
}
case ASM_MOVAPS: {
VarBytes prefix, opcode;
prefix = -1;
opcode = v->instr.variant == 2 ? 0x01000f29 : 0x01000f28;
assemblerrm(&v->instr, prefix, opcode, 1);
break;
}
case ASM_MOV:
assemblemov(&v->instr);
break;
case ASM_MOVQ:
switch (v->instr.variant) {
case 0:
assemblerrm(&v->instr, 0x66, 0x01000f7e, 0);
break;
case 1:
assemblerrm(&v->instr, 0x66, 0x01000f6e, 1);
break;
case 2:
assemblerrm(&v->instr, 0x66, 0x01000fd6, 0);
break;
case 3:
assemblerrm(&v->instr, 0xf3, 0x01000f7e, 1);
break;
default:
unreachable();
}
break;
case ASM_MOVSD:
assemblemovsmmx(&v->instr, 0xf2);
break;
case ASM_MOVSS:
assemblemovsmmx(&v->instr, 0xf3);
break;
case ASM_MOVSX: {
VarBytes opcode;
if (v->instr.variant >= 10) {
opcode = 0x63; // movsxd
} else {
static uint8_t variant2op[10] = {0xbe, 0xbe, 0xbe, 0xbf, 0xbf,
0xbe, 0xbe, 0xbe, 0xbf, 0xbf};
opcode = 0x01000f00 | variant2op[v->instr.variant];
}
assemblemovextend(&v->instr, opcode);
break;
}
case ASM_MOVZX: {
VarBytes opcode;
static uint8_t variant2op[10] = {0xb6, 0xb6, 0xb6, 0xb7, 0xb7,
0xb6, 0xb6, 0xb6, 0xb7, 0xb7};
opcode = 0x01000f00 | variant2op[v->instr.variant];
assemblemovextend(&v->instr, opcode);
break;
}
case ASM_ADD: {
static uint8_t variant2op[24] = {
0x04, 0x05, 0x05, 0x05, 0x80, 0x81, 0x81, 0x81,
0x80, 0x81, 0x81, 0x81, 0x02, 0x03, 0x03, 0x03,
0x00, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01, 0x01,
};
assemblebasicop(&v->instr, variant2op[v->instr.variant], 0x00);
break;
}
case ASM_AND: {
static uint8_t variant2op[24] = {
0x24, 0x25, 0x25, 0x25, 0x80, 0x81, 0x81, 0x81,
0x80, 0x81, 0x81, 0x81, 0x22, 0x23, 0x23, 0x23,
0x20, 0x21, 0x21, 0x21, 0x20, 0x21, 0x21, 0x21,
};
assemblebasicop(&v->instr, variant2op[v->instr.variant], 0x04);
break;
}
case ASM_CMP: {
static uint8_t variant2op[24] = {
0x3c, 0x3d, 0x3d, 0x3d, 0x80, 0x81, 0x81, 0x81,
0x80, 0x81, 0x81, 0x81, 0x3a, 0x3b, 0x3b, 0x3b,
0x38, 0x39, 0x39, 0x39, 0x38, 0x39, 0x39, 0x39,
};
assemblebasicop(&v->instr, variant2op[v->instr.variant], 0x07);
break;
}
case ASM_ADDSS:
assemblerrm(&v->instr, 0xf3, 0x01000f58, 1);
break;
case ASM_ADDSD:
assemblerrm(&v->instr, 0xf2, 0x01000f58, 1);
break;
case ASM_DIV:
assembledivmulneg(&v->instr, 0x06);
break;
case ASM_IDIV:
assembledivmulneg(&v->instr, 0x07);
break;
case ASM_DIVSD:
assemblerrm(&v->instr, 0xf2, 0x01000f5e, 1);
break;
case ASM_DIVSS:
assemblerrm(&v->instr, 0xf3, 0x01000f5e, 1);
break;
case ASM_MUL:
assembledivmulneg(&v->instr, 0x04);
break;
case ASM_MULSD:
assemblerrm(&v->instr, 0xf2, 0x01000f59, 1);
break;
case ASM_MULSS:
assemblerrm(&v->instr, 0xf3, 0x01000f59, 1);
break;
case ASM_IMUL:
assembleimul(&v->instr);
break;
case ASM_NEG:
assembledivmulneg(&v->instr, 0x03);
break;
case ASM_OR: {
static uint8_t variant2op[24] = {
0x0c, 0x0d, 0x0d, 0x0d, 0x80, 0x81, 0x81, 0x81,
0x80, 0x81, 0x81, 0x81, 0x0a, 0x0b, 0x0b, 0x0b,
0x08, 0x09, 0x09, 0x09, 0x08, 0x09, 0x09, 0x09,
};
assemblebasicop(&v->instr, variant2op[v->instr.variant], 0x01);
break;
}
case ASM_PXOR:
assemblerrm(&v->instr, 0x66, 0x01000fef, 1);
break;
case ASM_SET:
assembleset(&v->instr);
break;
case ASM_SAL:
/* fallthrough */
case ASM_SHL:
assembleshift(&v->instr, 0x04);
break;
case ASM_SAR:
assembleshift(&v->instr, 0x07);
break;
case ASM_SHR:
assembleshift(&v->instr, 0x05);
break;
case ASM_SUB: {
static uint8_t variant2op[24] = {
0x2c, 0x2d, 0x2d, 0x2d, 0x80, 0x81, 0x81, 0x81,
0x80, 0x81, 0x81, 0x81, 0x2a, 0x2b, 0x2b, 0x2b,
0x28, 0x29, 0x29, 0x29, 0x28, 0x29, 0x29, 0x29,
};
assemblebasicop(&v->instr, variant2op[v->instr.variant], 0x05);
break;
}
case ASM_SUBSS:
assemblerrm(&v->instr, 0xf3, 0x01000f5c, 1);
break;
case ASM_SUBSD:
assemblerrm(&v->instr, 0xf2, 0x01000f5c, 1);
break;
case ASM_TEST:
assembletest(&v->instr);
break;
case ASM_UCOMISD:
assemblerrm(&v->instr, 0x66, 0x01000f2e, 1);
break;
case ASM_UCOMISS:
assemblerrm(&v->instr, -1, 0x01000f2e, 1);
break;
case ASM_XORPD:
assemblerrm(&v->instr, 0x66, 0x01000f57, 1);
break;
case ASM_XORPS:
assemblerrm(&v->instr, -1, 0x01000f57, 1);
break;
case ASM_XOR: {
static uint8_t variant2op[24] = {
0x34, 0x35, 0x35, 0x35, 0x80, 0x81, 0x81, 0x81,
0x80, 0x81, 0x81, 0x81, 0x32, 0x33, 0x33, 0x33,
0x30, 0x31, 0x31, 0x31, 0x30, 0x31, 0x31, 0x31,
};
assemblebasicop(&v->instr, variant2op[v->instr.variant], 0x06);
break;
}
case ASM_XCHG: {
assemblexchg(&v->instr);
break;
}
default:
lfatal("assemble: unexpected kind: %d", v->kind);
}
}
}
/* 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;
int sbind;
stype = 0;
if (sym->defined) {
sbind = sym->global ? STB_GLOBAL : STB_LOCAL;
} else {
sbind = STB_GLOBAL;
}
sym->idx = symtab->hdr.sh_size / symtab->hdr.sh_entsize;
elfsym.st_name = elfstr(strtab, sym->name);
elfsym.st_value = sym->offset;
elfsym.st_size = sym->size;
elfsym.st_info = ELF64_ST_INFO(sbind, stype);
elfsym.st_shndx = sym->section ? sym->section->idx : SHN_UNDEF;
elfsym.st_other = 0;
secaddbytes(symtab, &elfsym, sizeof(Elf64_Sym));
}
static void fillsymtab(void) {
Symbol *sym;
size_t i;
// Local symbols
for (i = 0; i < symbols->cap; i++) {
if (!symbols->keys[i].str)
continue;
sym = symbols->vals[i];
if (!sym->defined || sym->global)
continue;
addtosymtab(sym);
}
// Global symbols
// Set start of global symbols.
symtab->hdr.sh_info = symtab->hdr.sh_size / symtab->hdr.sh_entsize;
for (i = 0; i < symbols->cap; i++) {
if (!symbols->keys[i].str)
continue;
sym = symbols->vals[i];
if (sym->defined && !sym->global)
continue;
addtosymtab(sym);
}
}
static int resolvereloc(Relocation *reloc) {
Symbol *sym;
uint8_t *rdata;
int64_t value;
sym = reloc->sym;
if (sym->section != reloc->section)
return 0;
switch (reloc->type) {
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;
rdata[0] = ((uint32_t)value & 0xff);
rdata[1] = ((uint32_t)value & 0xff00) >> 8;
rdata[2] = ((uint32_t)value & 0xff0000) >> 16;
rdata[3] = ((uint32_t)value & 0xff000000) >> 24;
return 1;
default:
unreachable();
return 0;
}
}
static void appendreloc(Relocation *reloc) {
Symbol *sym;
Section *relsection;
Elf64_Rela elfrel;
memset(&elfrel, 0, sizeof(elfrel));
sym = reloc->sym;
if (reloc->section == text)
relsection = textrel;
else if (reloc->section == data)
relsection = datarel;
else {
fatal("unexpected relocation for symbol '%s'", sym->name);
return;
}
switch (reloc->type) {
case R_X86_64_PC32:
case R_X86_64_32:
case R_X86_64_64:
elfrel.r_info = ELF64_R_INFO(sym->idx, reloc->type);
elfrel.r_offset = reloc->offset;
elfrel.r_addend = reloc->addend;
break;
default:
unreachable();
}
secaddbytes(relsection, &elfrel, sizeof(elfrel));
}
static void handlerelocs(void) {
Relocation *reloc;
size_t i;
for (i = 0; i < nrelocs; i++) {
reloc = &relocs[i];
if (resolvereloc(reloc))
continue;
appendreloc(reloc);
}
}
static void out(const void *buf, size_t n) {
fwrite(buf, 1, n, stdout);
if (ferror(stdout))
fatal("fwrite:");
}
static void outelf(void) {
size_t i;
uint64_t offset;
Elf64_Ehdr ehdr;
memset(&ehdr, 0, sizeof(ehdr));
ehdr.e_ident[0] = 0x7f;
ehdr.e_ident[1] = 'E';
ehdr.e_ident[2] = 'L';
ehdr.e_ident[3] = 'F';
ehdr.e_ident[4] = ELFCLASS64;
ehdr.e_ident[5] = ELFDATA2LSB;
ehdr.e_ident[6] = 1;
ehdr.e_type = ET_REL;
ehdr.e_machine = EM_X86_64;
ehdr.e_flags = 0;
ehdr.e_version = 1;
ehdr.e_ehsize = sizeof(Elf64_Ehdr);
ehdr.e_shoff = sizeof(Elf64_Ehdr);
ehdr.e_shentsize = sizeof(Elf64_Shdr);
ehdr.e_shnum = nsections;
ehdr.e_shstrndx = 1;
out(&ehdr, sizeof(ehdr));
offset = sizeof(Elf64_Ehdr) + sizeof(Elf64_Shdr) * nsections;
for (i = 0; i < nsections; i++) {
sections[i].hdr.sh_offset = offset;
out(§ions[i].hdr, sizeof(Elf64_Shdr));
offset += sections[i].hdr.sh_size;
}
for (i = 0; i < nsections; i++) {
if (sections[i].hdr.sh_type == SHT_NOBITS)
continue;
out(sections[i].data, sections[i].hdr.sh_size);
}
if (fflush(stdout) != 0)
fatal("fflush:");
}
static void usage(char *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);
}
static void parseargs(int argc, char *argv[]) {
char *a, *argv0, *outfname;
argv0 = argv[0];
for (++argv; *argv; argv++) {
if (argv[0][0] != '-')
break;
for (a = &argv[0][1]; *a; a++) {
switch (*a) {
case '-':
case 'h':
usage(argv0);
break;
case 'r':
nrelax = atoi(*++argv);
break;
case 'o':
if (argv[1] == NULL)
usage(argv0);
outfname = *++argv;
if (!freopen(outfname, "w", stdout))
fatal("unable to open %s:", outfname);
break;
default:
usage(argv0);
}
}
}
if (argv[0]) {
if (argv[1])
usage(argv0);
infilename = argv[0];
if (!freopen(infilename, "r", stdin))
fatal("unable to open %s:", infilename);
}
}
int main(int argc, char *argv[]) {
symbols = mkhtab(256);
parseargs(argc, argv);
allasm = parseasm();
initsections();
assemble();
while (nrelax-- > 0) {
relaxreset();
assemble();
}
fillsymtab();
handlerelocs();
outelf();
return 0;
}
|