This repository was archived by the owner on Aug 16, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_emitter.c
More file actions
1101 lines (971 loc) · 35.9 KB
/
Copy pathbinary_emitter.c
File metadata and controls
1101 lines (971 loc) · 35.9 KB
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
#include "binary_emitter.h"
#include "binary_emitter_internal.h"
#include "../common.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BINARY_SECTION_INDEX_NONE ((size_t)-1)
#define COFF_MACHINE_AMD64 0x8664
#define COFF_STORAGE_CLASS_EXTERNAL 2
#define COFF_STORAGE_CLASS_STATIC 3
#define COFF_SYMBOL_RECORD_SIZE 18u
#define COFF_SECTION_TEXT_CHARACTERISTICS 0x60000020u
#define COFF_SECTION_RDATA_CHARACTERISTICS 0x40000040u
#define COFF_SECTION_DATA_CHARACTERISTICS 0xC0000040u
#define COFF_SECTION_BSS_CHARACTERISTICS 0xC0000080u
#define COFF_RELOC_AMD64_ADDR64 0x0001
#define COFF_RELOC_AMD64_ADDR32NB 0x0003
#define COFF_RELOC_AMD64_REL32 0x0004
#define COFF_RELOC_AMD64_SECREL 0x000Bu
static void binary_emitter_set_error(BinaryEmitter *emitter,
const char *message) {
if (!emitter || !message) {
return;
}
free(emitter->error_message);
emitter->error_message = mettle_strdup(message);
}
static void binary_section_clear(BinarySection *section) {
if (!section) {
return;
}
free(section->name);
free(section->data);
memset(section, 0, sizeof(*section));
}
static void binary_symbol_clear(BinarySymbol *symbol) {
if (!symbol) {
return;
}
free(symbol->name);
memset(symbol, 0, sizeof(*symbol));
}
static void binary_relocation_clear(BinaryRelocation *relocation) {
if (!relocation) {
return;
}
free(relocation->symbol_name);
memset(relocation, 0, sizeof(*relocation));
}
static int binary_emitter_reserve_sections(BinaryEmitter *emitter,
size_t minimum_capacity) {
if (!emitter) {
return 0;
}
if (emitter->section_capacity >= minimum_capacity) {
return 1;
}
size_t new_capacity = emitter->section_capacity ? emitter->section_capacity : 4;
while (new_capacity < minimum_capacity) {
new_capacity *= 2;
}
BinarySection *grown =
realloc(emitter->sections, new_capacity * sizeof(BinarySection));
if (!grown) {
binary_emitter_set_error(emitter,
"Out of memory while growing section table");
return 0;
}
for (size_t i = emitter->section_capacity; i < new_capacity; i++) {
memset(&grown[i], 0, sizeof(grown[i]));
}
emitter->sections = grown;
emitter->section_capacity = new_capacity;
return 1;
}
static int binary_emitter_reserve_symbols(BinaryEmitter *emitter,
size_t minimum_capacity) {
if (!emitter) {
return 0;
}
if (emitter->symbol_capacity >= minimum_capacity) {
return 1;
}
size_t new_capacity = emitter->symbol_capacity ? emitter->symbol_capacity : 8;
while (new_capacity < minimum_capacity) {
new_capacity *= 2;
}
BinarySymbol *grown =
realloc(emitter->symbols, new_capacity * sizeof(BinarySymbol));
if (!grown) {
binary_emitter_set_error(emitter,
"Out of memory while growing symbol table");
return 0;
}
for (size_t i = emitter->symbol_capacity; i < new_capacity; i++) {
memset(&grown[i], 0, sizeof(grown[i]));
grown[i].section_index = BINARY_SECTION_INDEX_NONE;
}
emitter->symbols = grown;
emitter->symbol_capacity = new_capacity;
return 1;
}
static int binary_emitter_reserve_relocations(BinaryEmitter *emitter,
size_t minimum_capacity) {
if (!emitter) {
return 0;
}
if (emitter->relocation_capacity >= minimum_capacity) {
return 1;
}
size_t new_capacity =
emitter->relocation_capacity ? emitter->relocation_capacity : 8;
while (new_capacity < minimum_capacity) {
new_capacity *= 2;
}
BinaryRelocation *grown =
realloc(emitter->relocations, new_capacity * sizeof(BinaryRelocation));
if (!grown) {
binary_emitter_set_error(emitter,
"Out of memory while growing relocation table");
return 0;
}
for (size_t i = emitter->relocation_capacity; i < new_capacity; i++) {
memset(&grown[i], 0, sizeof(grown[i]));
}
emitter->relocations = grown;
emitter->relocation_capacity = new_capacity;
return 1;
}
static int binary_section_reserve(BinaryEmitter *emitter, BinarySection *section,
size_t minimum_capacity) {
if (!emitter || !section) {
return 0;
}
if (section->capacity >= minimum_capacity) {
return 1;
}
size_t new_capacity = section->capacity ? section->capacity : 32;
while (new_capacity < minimum_capacity) {
new_capacity *= 2;
}
unsigned char *grown = realloc(section->data, new_capacity);
if (!grown) {
binary_emitter_set_error(emitter,
"Out of memory while growing section payload");
return 0;
}
section->data = grown;
section->capacity = new_capacity;
return 1;
}
static uint64_t binary_emitter_hash_name(const char *name) {
/* FNV-1a 64-bit. unsigned long is 32-bit on Windows, so use a fixed width. */
uint64_t hash = 1469598103934665603ULL;
for (const unsigned char *p = (const unsigned char *)name; *p; p++) {
hash ^= (uint64_t)*p;
hash *= 1099511628211ULL;
}
return hash;
}
/* Rebuilds the symbol hash index from scratch over the current symbol array.
* Called when the index would exceed a 0.7 load factor. */
static int binary_emitter_symbol_index_rehash(BinaryEmitter *emitter,
size_t new_bucket_count) {
size_t *buckets = calloc(new_bucket_count, sizeof(size_t));
if (!buckets) {
return 0;
}
size_t mask = new_bucket_count - 1;
for (size_t i = 0; i < emitter->symbol_count; i++) {
if (!emitter->symbols[i].name) {
continue;
}
size_t pos =
(size_t)(binary_emitter_hash_name(emitter->symbols[i].name) &
(uint64_t)mask);
while (buckets[pos] != 0) {
pos = (pos + 1) & mask;
}
buckets[pos] = i + 1;
}
free(emitter->symbol_index_buckets);
emitter->symbol_index_buckets = buckets;
emitter->symbol_index_bucket_count = new_bucket_count;
return 1;
}
/* Records that emitter->symbols[symbol_index] now exists in the hash index.
* Grows the bucket array first if needed. */
static int binary_emitter_symbol_index_insert(BinaryEmitter *emitter,
size_t symbol_index) {
size_t live = symbol_index + 1;
if (emitter->symbol_index_bucket_count == 0 ||
(live * 10) >= (emitter->symbol_index_bucket_count * 7)) {
size_t next = emitter->symbol_index_bucket_count == 0
? 256
: emitter->symbol_index_bucket_count * 2;
if (!binary_emitter_symbol_index_rehash(emitter, next)) {
return 0;
}
/* Rehash already placed every existing symbol, including this one if it
* was appended before the call. Re-insert below is still safe because the
* caller invokes this exactly once per new symbol. */
}
size_t mask = emitter->symbol_index_bucket_count - 1;
const char *name = emitter->symbols[symbol_index].name;
size_t pos = (size_t)(binary_emitter_hash_name(name) & (uint64_t)mask);
while (emitter->symbol_index_buckets[pos] != 0) {
if (emitter->symbol_index_buckets[pos] == symbol_index + 1) {
return 1; /* already present (placed by a rehash) */
}
pos = (pos + 1) & mask;
}
emitter->symbol_index_buckets[pos] = symbol_index + 1;
return 1;
}
static int binary_emitter_find_symbol_index(const BinaryEmitter *emitter,
const char *name) {
if (!emitter || !name) {
return -1;
}
if (emitter->symbol_index_buckets &&
emitter->symbol_index_bucket_count > 0) {
size_t mask = emitter->symbol_index_bucket_count - 1;
size_t pos =
(size_t)(binary_emitter_hash_name(name) & (uint64_t)mask);
while (emitter->symbol_index_buckets[pos] != 0) {
size_t idx = emitter->symbol_index_buckets[pos] - 1;
if (emitter->symbols[idx].name &&
strcmp(emitter->symbols[idx].name, name) == 0) {
return (int)idx;
}
pos = (pos + 1) & mask;
}
return -1;
}
/* Index not built yet (no symbols defined) — linear fallback. */
for (size_t i = 0; i < emitter->symbol_count; i++) {
if (emitter->symbols[i].name &&
strcmp(emitter->symbols[i].name, name) == 0) {
return (int)i;
}
}
return -1;
}
static int binary_emitter_write_u16(FILE *file, uint16_t value) {
return fwrite(&value, sizeof(value), 1, file) == 1;
}
static int binary_emitter_write_u32(FILE *file, uint32_t value) {
return fwrite(&value, sizeof(value), 1, file) == 1;
}
static int binary_emitter_write_i16(FILE *file, int16_t value) {
return fwrite(&value, sizeof(value), 1, file) == 1;
}
static int binary_emitter_write_section_name(FILE *file, const char *name,
size_t string_table_offset) {
unsigned char field[8] = {0};
if (!name) {
return fwrite(field, sizeof(field), 1, file) == 1;
}
size_t length = strlen(name);
if (length <= sizeof(field)) {
memcpy(field, name, length);
return fwrite(field, sizeof(field), 1, file) == 1;
}
int written = snprintf((char *)field, sizeof(field), "/%zu",
string_table_offset);
if (written <= 0 || (size_t)written >= sizeof(field)) {
return 0;
}
return fwrite(field, sizeof(field), 1, file) == 1;
}
static int binary_emitter_write_symbol_name(FILE *file, const char *name,
size_t string_table_offset) {
unsigned char field[8] = {0};
if (!name) {
return fwrite(field, sizeof(field), 1, file) == 1;
}
size_t length = strlen(name);
if (length <= sizeof(field)) {
memcpy(field, name, length);
return fwrite(field, sizeof(field), 1, file) == 1;
}
uint32_t zero_prefix = 0;
uint32_t offset = (uint32_t)string_table_offset;
memcpy(field, &zero_prefix, sizeof(zero_prefix));
memcpy(field + sizeof(zero_prefix), &offset, sizeof(offset));
return fwrite(field, sizeof(field), 1, file) == 1;
}
static int binary_emitter_write_zero_bytes(FILE *file, size_t count) {
unsigned char zeroes[COFF_SYMBOL_RECORD_SIZE] = {0};
while (count > 0) {
size_t chunk = count < sizeof(zeroes) ? count : sizeof(zeroes);
if (fwrite(zeroes, 1, chunk, file) != chunk) {
return 0;
}
count -= chunk;
}
return 1;
}
static uint32_t binary_emitter_default_section_characteristics(
BinarySectionKind kind) {
switch (kind) {
case BINARY_SECTION_TEXT:
return COFF_SECTION_TEXT_CHARACTERISTICS;
case BINARY_SECTION_RDATA:
case BINARY_SECTION_INIT_ARRAY:
case BINARY_SECTION_FINI_ARRAY:
case BINARY_SECTION_DEBUG:
return COFF_SECTION_RDATA_CHARACTERISTICS;
case BINARY_SECTION_DATA:
return COFF_SECTION_DATA_CHARACTERISTICS;
case BINARY_SECTION_BSS:
return COFF_SECTION_BSS_CHARACTERISTICS;
default:
return COFF_SECTION_DATA_CHARACTERISTICS;
}
}
static uint16_t binary_emitter_map_relocation_kind(BinaryRelocationKind kind) {
switch (kind) {
case BINARY_RELOCATION_ADDR64:
return COFF_RELOC_AMD64_ADDR64;
case BINARY_RELOCATION_ADDR32NB:
return COFF_RELOC_AMD64_ADDR32NB;
case BINARY_RELOCATION_SECTION_REL32:
return COFF_RELOC_AMD64_SECREL;
case BINARY_RELOCATION_REL32:
default:
return COFF_RELOC_AMD64_REL32;
}
}
BinaryTargetFormat binary_target_format_host_default(void) {
#ifdef _WIN32
return BINARY_TARGET_FORMAT_COFF_WIN64;
#elif defined(__aarch64__) && defined(__linux__)
return BINARY_TARGET_FORMAT_ELF_ARM64;
#else
return BINARY_TARGET_FORMAT_ELF_X64;
#endif
}
BinaryEmitter *binary_emitter_create(BinaryTargetFormat target_format) {
BinaryEmitter *emitter = calloc(1, sizeof(BinaryEmitter));
if (!emitter) {
return NULL;
}
emitter->target_format = target_format;
return emitter;
}
void binary_emitter_reset(BinaryEmitter *emitter) {
if (!emitter) {
return;
}
for (size_t i = 0; i < emitter->section_count; i++) {
binary_section_clear(&emitter->sections[i]);
}
for (size_t i = 0; i < emitter->symbol_count; i++) {
binary_symbol_clear(&emitter->symbols[i]);
}
for (size_t i = 0; i < emitter->relocation_count; i++) {
binary_relocation_clear(&emitter->relocations[i]);
}
emitter->section_count = 0;
emitter->symbol_count = 0;
emitter->relocation_count = 0;
free(emitter->symbol_index_buckets);
emitter->symbol_index_buckets = NULL;
emitter->symbol_index_bucket_count = 0;
free(emitter->error_message);
emitter->error_message = NULL;
}
void binary_emitter_destroy(BinaryEmitter *emitter) {
if (!emitter) {
return;
}
binary_emitter_reset(emitter);
free(emitter->sections);
free(emitter->symbols);
free(emitter->relocations);
free(emitter);
}
size_t binary_emitter_get_or_create_section(BinaryEmitter *emitter,
const char *name,
BinarySectionKind kind,
uint32_t characteristics,
size_t alignment) {
if (!emitter || !name || name[0] == '\0') {
return BINARY_SECTION_INDEX_NONE;
}
for (size_t i = 0; i < emitter->section_count; i++) {
if (emitter->sections[i].name &&
strcmp(emitter->sections[i].name, name) == 0) {
if (alignment > emitter->sections[i].alignment) {
emitter->sections[i].alignment = alignment;
}
if (characteristics != 0) {
emitter->sections[i].characteristics = characteristics;
}
return i;
}
}
if (!binary_emitter_reserve_sections(emitter, emitter->section_count + 1)) {
return BINARY_SECTION_INDEX_NONE;
}
BinarySection *section = &emitter->sections[emitter->section_count];
memset(section, 0, sizeof(*section));
section->name = mettle_strdup(name);
if (!section->name) {
binary_emitter_set_error(emitter,
"Out of memory while storing section name");
return BINARY_SECTION_INDEX_NONE;
}
section->kind = kind;
section->characteristics = characteristics;
section->alignment = alignment ? alignment : 1;
emitter->section_count++;
return emitter->section_count - 1;
}
BinarySection *binary_emitter_get_section(BinaryEmitter *emitter,
size_t section_index) {
if (!emitter || section_index >= emitter->section_count) {
return NULL;
}
return &emitter->sections[section_index];
}
const BinarySection *binary_emitter_get_section_const(
const BinaryEmitter *emitter, size_t section_index) {
if (!emitter || section_index >= emitter->section_count) {
return NULL;
}
return &emitter->sections[section_index];
}
int binary_emitter_align_section(BinaryEmitter *emitter, size_t section_index,
size_t alignment, unsigned char fill_byte) {
BinarySection *section = binary_emitter_get_section(emitter, section_index);
if (!section || alignment == 0) {
return 0;
}
if (alignment > section->alignment) {
section->alignment = alignment;
}
size_t remainder = section->size % alignment;
if (remainder == 0) {
return 1;
}
size_t padding = alignment - remainder;
if (!binary_section_reserve(emitter, section, section->size + padding)) {
return 0;
}
memset(section->data + section->size, fill_byte, padding);
section->size += padding;
if (section->virtual_size < section->size) {
section->virtual_size = section->size;
}
return 1;
}
int binary_emitter_append_bytes(BinaryEmitter *emitter, size_t section_index,
const void *data, size_t size,
size_t *offset_out) {
BinarySection *section = binary_emitter_get_section(emitter, section_index);
if (!section || (!data && size != 0)) {
return 0;
}
if (!binary_section_reserve(emitter, section, section->size + size)) {
return 0;
}
if (offset_out) {
*offset_out = section->size;
}
if (size != 0) {
memcpy(section->data + section->size, data, size);
section->size += size;
}
if (section->virtual_size < section->size) {
section->virtual_size = section->size;
}
return 1;
}
int binary_emitter_append_zeros(BinaryEmitter *emitter, size_t section_index,
size_t size, size_t *offset_out) {
BinarySection *section = binary_emitter_get_section(emitter, section_index);
if (!section) {
return 0;
}
if (!binary_section_reserve(emitter, section, section->size + size)) {
return 0;
}
if (offset_out) {
*offset_out = section->size;
}
if (size != 0) {
memset(section->data + section->size, 0, size);
section->size += size;
}
if (section->virtual_size < section->size) {
section->virtual_size = section->size;
}
return 1;
}
int binary_emitter_set_section_virtual_size(BinaryEmitter *emitter,
size_t section_index,
size_t virtual_size) {
BinarySection *section = binary_emitter_get_section(emitter, section_index);
if (!section || virtual_size < section->size) {
return 0;
}
section->virtual_size = virtual_size;
return 1;
}
int binary_emitter_define_symbol(BinaryEmitter *emitter, const char *name,
BinarySymbolBinding binding,
size_t section_index, size_t value,
size_t size) {
if (!emitter || !name || name[0] == '\0') {
return 0;
}
int existing_index = binary_emitter_find_symbol_index(emitter, name);
if (existing_index >= 0) {
BinarySymbol *symbol = &emitter->symbols[(size_t)existing_index];
symbol->binding = binding;
symbol->section_index = section_index;
symbol->value = value;
symbol->size = size;
return 1;
}
if (!binary_emitter_reserve_symbols(emitter, emitter->symbol_count + 1)) {
return 0;
}
BinarySymbol *symbol = &emitter->symbols[emitter->symbol_count];
memset(symbol, 0, sizeof(*symbol));
symbol->name = mettle_strdup(name);
if (!symbol->name) {
binary_emitter_set_error(emitter,
"Out of memory while storing symbol name");
return 0;
}
symbol->binding = binding;
symbol->section_index = section_index;
symbol->value = value;
symbol->size = size;
size_t new_index = emitter->symbol_count;
emitter->symbol_count++;
if (!binary_emitter_symbol_index_insert(emitter, new_index)) {
emitter->symbol_count--;
free(symbol->name);
symbol->name = NULL;
binary_emitter_set_error(emitter,
"Out of memory while indexing symbol name");
return 0;
}
return 1;
}
int binary_emitter_declare_external(BinaryEmitter *emitter, const char *name) {
return binary_emitter_define_symbol(emitter, name, BINARY_SYMBOL_EXTERNAL,
BINARY_SECTION_INDEX_NONE, 0, 0);
}
const BinarySymbol *binary_emitter_find_symbol(const BinaryEmitter *emitter,
const char *name) {
int index = binary_emitter_find_symbol_index(emitter, name);
if (index < 0) {
return NULL;
}
return &emitter->symbols[(size_t)index];
}
int binary_emitter_add_relocation(BinaryEmitter *emitter, size_t section_index,
size_t offset, BinaryRelocationKind kind,
const char *symbol_name, int32_t addend) {
if (!emitter || !symbol_name || symbol_name[0] == '\0' ||
section_index >= emitter->section_count) {
return 0;
}
if (!binary_emitter_reserve_relocations(emitter,
emitter->relocation_count + 1)) {
return 0;
}
BinaryRelocation *relocation =
&emitter->relocations[emitter->relocation_count];
memset(relocation, 0, sizeof(*relocation));
relocation->symbol_name = mettle_strdup(symbol_name);
if (!relocation->symbol_name) {
binary_emitter_set_error(emitter,
"Out of memory while storing relocation symbol");
return 0;
}
relocation->section_index = section_index;
relocation->offset = offset;
relocation->kind = kind;
relocation->addend = addend;
emitter->relocation_count++;
return 1;
}
const char *binary_emitter_get_error(const BinaryEmitter *emitter) {
return emitter ? emitter->error_message : NULL;
}
static int binary_emitter_write_coff_object_file(BinaryEmitter *emitter,
const char *filename) {
if (emitter->section_count > 0xFFFFu) {
binary_emitter_set_error(emitter, "Too many sections for COFF object file");
return 0;
}
if (emitter->relocation_count > 0xFFFFFFFFu ||
emitter->symbol_count > 0xFFFFFFFFu) {
binary_emitter_set_error(emitter, "Emitter tables exceed COFF limits");
return 0;
}
if (emitter->section_count >
((0xFFFFFFFFu - (uint32_t)emitter->symbol_count) / 2u)) {
binary_emitter_set_error(emitter,
"Section symbol records exceed COFF limits");
return 0;
}
FILE *file = fopen(filename, "wb");
if (!file) {
binary_emitter_set_error(emitter, "Failed to open object output file");
return 0;
}
/* The COFF writer emits the header, section table, and symbol table as many
* tiny 2/4-byte fwrites. A large stdio buffer collapses those into memory
* copies instead of one host write call per field. */
setvbuf(file, NULL, _IOFBF, 1 << 20);
uint32_t *section_name_offsets = NULL;
uint32_t *symbol_name_offsets = NULL;
uint32_t *section_raw_offsets = NULL;
uint32_t *section_reloc_offsets = NULL;
uint32_t *section_reloc_counts = NULL;
uint32_t *symbol_table_indices = NULL;
uint32_t string_table_size = 4;
uint32_t total_symbol_records = 0;
int ok = 0;
if (emitter->section_count > 0) {
section_name_offsets = calloc(emitter->section_count, sizeof(uint32_t));
section_raw_offsets = calloc(emitter->section_count, sizeof(uint32_t));
section_reloc_offsets = calloc(emitter->section_count, sizeof(uint32_t));
section_reloc_counts = calloc(emitter->section_count, sizeof(uint32_t));
}
if (emitter->symbol_count > 0) {
symbol_name_offsets = calloc(emitter->symbol_count, sizeof(uint32_t));
symbol_table_indices = calloc(emitter->symbol_count, sizeof(uint32_t));
}
if ((emitter->section_count > 0 &&
(!section_name_offsets || !section_raw_offsets ||
!section_reloc_offsets || !section_reloc_counts)) ||
(emitter->symbol_count > 0 &&
(!symbol_name_offsets || !symbol_table_indices))) {
binary_emitter_set_error(emitter,
"Out of memory while preparing COFF tables");
goto cleanup;
}
for (size_t i = 0; i < emitter->section_count; i++) {
const BinarySection *section = &emitter->sections[i];
if (section->name && strlen(section->name) > 8) {
section_name_offsets[i] = string_table_size;
string_table_size += (uint32_t)strlen(section->name) + 1;
}
}
for (size_t i = 0; i < emitter->symbol_count; i++) {
const BinarySymbol *symbol = &emitter->symbols[i];
if (symbol->name && strlen(symbol->name) > 8) {
symbol_name_offsets[i] = string_table_size;
string_table_size += (uint32_t)strlen(symbol->name) + 1;
}
symbol_table_indices[i] = (uint32_t)i;
}
total_symbol_records =
(uint32_t)emitter->symbol_count + (uint32_t)(emitter->section_count * 2u);
for (size_t i = 0; i < emitter->relocation_count; i++) {
const BinaryRelocation *relocation = &emitter->relocations[i];
if (relocation->section_index >= emitter->section_count) {
binary_emitter_set_error(emitter,
"Relocation refers to an invalid section");
goto cleanup;
}
section_reloc_counts[relocation->section_index]++;
}
/* COFF stores a section's relocation count in a 16-bit field. When a section
* has more than 0xFFFF relocations we use the IMAGE_SCN_LNK_NRELOC_OVFL
* mechanism: the count field is set to 0xFFFF, the flag is set in the section
* characteristics, and a synthetic first relocation record carries the real
* count (+1, to include itself) in its VirtualAddress. */
uint32_t reloc_overflow_max = 0xFFFFu;
for (size_t i = 0; i < emitter->section_count; i++) {
if (section_reloc_counts[i] > reloc_overflow_max) {
reloc_overflow_max = section_reloc_counts[i];
}
}
/* A section with exactly 0xFFFF real relocations would be ambiguous with the
* overflow sentinel, so it must also use the overflow form. */
uint32_t offset = 20u + (uint32_t)(emitter->section_count * 40u);
for (size_t i = 0; i < emitter->section_count; i++) {
const BinarySection *section = &emitter->sections[i];
if (section->kind != BINARY_SECTION_BSS && section->size > 0) {
section_raw_offsets[i] = offset;
offset += (uint32_t)section->size;
}
}
for (size_t i = 0; i < emitter->section_count; i++) {
if (section_reloc_counts[i] > 0) {
section_reloc_offsets[i] = offset;
uint32_t records = section_reloc_counts[i];
if (records >= 0xFFFFu) {
records += 1u; /* synthetic overflow-count record */
}
offset += records * 10u;
}
}
uint32_t pointer_to_symbol_table = offset;
if (!binary_emitter_write_u16(file, COFF_MACHINE_AMD64) ||
!binary_emitter_write_u16(file, (uint16_t)emitter->section_count) ||
!binary_emitter_write_u32(file, 0) ||
!binary_emitter_write_u32(file, pointer_to_symbol_table) ||
!binary_emitter_write_u32(file, total_symbol_records) ||
!binary_emitter_write_u16(file, 0) ||
!binary_emitter_write_u16(file, 0)) {
binary_emitter_set_error(emitter, "Failed while writing COFF file header");
goto cleanup;
}
for (size_t i = 0; i < emitter->section_count; i++) {
const BinarySection *section = &emitter->sections[i];
uint32_t characteristics = section->characteristics;
if (characteristics == 0) {
characteristics =
binary_emitter_default_section_characteristics(section->kind);
}
/* >= 0xFFFF relocations -> overflow form: 0xFFFF in the count field plus the
* IMAGE_SCN_LNK_NRELOC_OVFL (0x01000000) flag. */
uint16_t reloc_count_field = (uint16_t)section_reloc_counts[i];
if (section_reloc_counts[i] >= 0xFFFFu) {
reloc_count_field = 0xFFFFu;
characteristics |= 0x01000000u;
}
if (!binary_emitter_write_section_name(file, section->name,
section_name_offsets[i]) ||
!binary_emitter_write_u32(file, 0) ||
!binary_emitter_write_u32(file, 0) ||
!binary_emitter_write_u32(
file, section->kind == BINARY_SECTION_BSS ? 0u : (uint32_t)section->size) ||
!binary_emitter_write_u32(file, section_raw_offsets[i]) ||
!binary_emitter_write_u32(file, section_reloc_offsets[i]) ||
!binary_emitter_write_u32(file, 0) ||
!binary_emitter_write_u16(file, reloc_count_field) ||
!binary_emitter_write_u16(file, 0) ||
!binary_emitter_write_u32(file, characteristics)) {
binary_emitter_set_error(emitter,
"Failed while writing COFF section headers");
goto cleanup;
}
}
for (size_t i = 0; i < emitter->section_count; i++) {
const BinarySection *section = &emitter->sections[i];
if (section->kind == BINARY_SECTION_BSS || section->size == 0) {
continue;
}
if (fwrite(section->data, 1, section->size, file) != section->size) {
binary_emitter_set_error(emitter,
"Failed while writing COFF section payload");
goto cleanup;
}
}
/* Emit relocations grouped by section. The previous implementation rescanned
* every relocation once per section (O(sections * relocations)). Instead do
* a single counting sort: compute each section's start index in a combined
* ordering, then place every relocation in one O(relocations) pass. Combined
* with the O(1) symbol-name hash index this drops the whole step from
* O(sections * relocations * symbols) to O(relocations). */
if (emitter->relocation_count > 0) {
size_t *section_reloc_start =
calloc(emitter->section_count + 1, sizeof(size_t));
size_t *ordered_relocations =
calloc(emitter->relocation_count, sizeof(size_t));
if (!section_reloc_start || !ordered_relocations) {
free(section_reloc_start);
free(ordered_relocations);
binary_emitter_set_error(emitter,
"Out of memory while ordering relocations");
goto cleanup;
}
/* Prefix sums of per-section counts give each section's slot range. */
for (size_t i = 0; i < emitter->section_count; i++) {
section_reloc_start[i + 1] =
section_reloc_start[i] + section_reloc_counts[i];
}
/* Stable bucket placement preserves original within-section order. */
size_t *cursor = calloc(emitter->section_count, sizeof(size_t));
if (!cursor) {
free(section_reloc_start);
free(ordered_relocations);
binary_emitter_set_error(emitter,
"Out of memory while ordering relocations");
goto cleanup;
}
for (size_t r = 0; r < emitter->relocation_count; r++) {
size_t sec = emitter->relocations[r].section_index;
size_t dst = section_reloc_start[sec] + cursor[sec]++;
ordered_relocations[dst] = r;
}
free(cursor);
/* Write relocations per section so an overflow section can be prefixed by
* its synthetic count record (VirtualAddress = real count + 1). */
int order_ok = 1;
for (size_t s = 0; s < emitter->section_count && order_ok; s++) {
if (section_reloc_counts[s] == 0) {
continue;
}
if (section_reloc_counts[s] >= 0xFFFFu) {
if (!binary_emitter_write_u32(file, section_reloc_counts[s] + 1u) ||
!binary_emitter_write_u32(file, 0) ||
!binary_emitter_write_u16(file, 0)) {
binary_emitter_set_error(
emitter, "Failed while writing COFF relocation overflow record");
order_ok = 0;
break;
}
}
size_t k = section_reloc_start[s];
size_t k_end = section_reloc_start[s + 1];
for (; k < k_end && order_ok; k++) {
const BinaryRelocation *relocation =
&emitter->relocations[ordered_relocations[k]];
int symbol_index =
binary_emitter_find_symbol_index(emitter, relocation->symbol_name);
if (symbol_index < 0) {
char error_buffer[256];
snprintf(error_buffer, sizeof(error_buffer),
"Relocation refers to an undefined symbol '%s'",
relocation->symbol_name ? relocation->symbol_name : "<null>");
binary_emitter_set_error(emitter, error_buffer);
order_ok = 0;
break;
}
if (!binary_emitter_write_u32(file, (uint32_t)relocation->offset) ||
!binary_emitter_write_u32(
file, symbol_table_indices[(size_t)symbol_index]) ||
!binary_emitter_write_u16(
file, binary_emitter_map_relocation_kind(relocation->kind))) {
binary_emitter_set_error(emitter,
"Failed while writing COFF relocations");
order_ok = 0;
break;
}
}
}
free(section_reloc_start);
free(ordered_relocations);
if (!order_ok) {
goto cleanup;
}
}
for (size_t i = 0; i < emitter->symbol_count; i++) {
const BinarySymbol *symbol = &emitter->symbols[i];
int16_t section_number = 0;
uint16_t type = 0;
unsigned char storage_class = COFF_STORAGE_CLASS_EXTERNAL;
if (symbol->binding == BINARY_SYMBOL_LOCAL) {
storage_class = COFF_STORAGE_CLASS_STATIC;
}
if (symbol->section_index != BINARY_SECTION_INDEX_NONE) {
if (symbol->section_index >= emitter->section_count) {
binary_emitter_set_error(emitter,
"Symbol refers to an invalid section");
goto cleanup;
}
section_number = (int16_t)(symbol->section_index + 1);
if (emitter->sections[symbol->section_index].kind == BINARY_SECTION_TEXT) {
type = 0x0020u;
}
}
if (!binary_emitter_write_symbol_name(file, symbol->name,
symbol_name_offsets[i]) ||
!binary_emitter_write_u32(file, (uint32_t)symbol->value) ||
!binary_emitter_write_i16(file, section_number) ||
!binary_emitter_write_u16(file, type) ||
fwrite(&storage_class, sizeof(storage_class), 1, file) != 1) {
binary_emitter_set_error(emitter,
"Failed while writing COFF symbols");
goto cleanup;
}
unsigned char aux_symbols = 0;
if (fwrite(&aux_symbols, sizeof(aux_symbols), 1, file) != 1) {
binary_emitter_set_error(emitter,
"Failed while finalizing COFF symbols");