-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultilink.c
3967 lines (3313 loc) · 107 KB
/
multilink.c
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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* The contents of this file are subject to the Mozilla Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is Multilink.
*
* The Initial Developer of the Original Code is AvantGo, Inc.
* Portions created by AvantGo, Inc. are Copyright (C) 1998-1999
* AvantGo, Inc. All Rights Reserved.
*
* multilink.c
*
* created: David Williams, [email protected], July 15, 1998.
*
* Based in part on:
* obj-res, by Dionne & Associates
* Gcc PalmOS runtime support by Kresten Krab Thorup
* gtscc by David Williams (see www.mozilla.org)
*
* Multilink makes it possible to link large, multiple code segment
* PalmOS applications. It can also create multiple code segment PalmOS
* SysLib*() compatible libraries.
*
* Multilink is a linker driver program. It sets up all the link objects,
* application data, and inter segment call glue, but relies on gcc to do
* the actual work of linking. Multilink is non-invasive to the gcc
* compile phase. That is, no special code is generated for an
* inter-segment call. Gcc is used to fully link each code segment, and
* so is called at least N times for one multilink, where N is the number
* of code segments.
*
* Cross segment calls are achieved by having a local proxy (or
* surrogate) function for all "outside" segment functions. The assembler
* generated surrogate function keeps the local arguments intact, and
* makes the cross segment jump to the "real" function via a simple
* indexed jump table that exists in each segment. The jump tables are
* generated in the code segment. There is a small table of these jump
* tables, and this is allocated in system heap memory. A pointer to this
* table of jump tables is kept in the global data segment. In addition a
* pointer to the table of jmp tables is kept in a PalmOS Ftr, so that a
* cross segment call can be made even when no globals are present.
*
* Multilink generates one data segment that is shared by all code
* segments. This is done via extreme linker gymnastics. Multilink
* analyses all the source objects, keeps track of all data references,
* and then feeds the linker a series of dummy data with each code
* segment. The dummy data is added before and after the real data of the
* code segment and serves the purpose of "getting all the offsets" to
* match. That is, all global references to (say) Foo use the same a4
* relative offset. In addition, initialized data from each code segment
* is merged into the same final data segment.
*
* Multilink replaces both gcc as linker, and obj-res in the traditional
* single segment gcc build process. Multilink actually calls obj-res for
* each code segment, then merges the results for global data, and
* renames everything else according to the basename argument - djw.
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <arpa/inet.h>
#include <bfd.h>
#include "multilinkrloc.h"
#define MULTILINK_VERSION 0x00000300L /* 00MMmmuu = MM.mm[.uu] */
/*
* Version history.
* 00000300 Release version number. .coff to .out, removed need for -basename.
* 00000213 Documentation cleanup.
* 00000212 Fixed 2.11 for 0.5 tool chain (needed to set A4 from A5).
* 00000211 In startup (crt0): Preserve app globals in sub calls.
* 00000210 Reimplemented support standard libraries to make them work
* just like user libraries.
* 0000020F Added support for stdlib globals.
* 0000020E Added hack support for -Ldir.
* 0000020D Added support for libraries (lib.a).
* 0000020C Added code that links stdlibs into one only segment. Changed
* semantics of -stdlib, -nostdlib. Added -lgcc, -lc.
* 0000020B Added -gdb-script option. Fixed POSE complaint in crt0.c.
* 0000020A New "a5 always on" runtime model for a5 mode. Patches
* to gdb startup code for a5 world.
* 00000209 Windows patches integration.
* 00000208 Reference count the code jmptables.
* 00000207 Added tool side patching of the section vma addresses.
* 00000206 Support for gcc 2.95 (egcs) added. Lots rewritten. What was
* handled by obj-res is now handled inline. Simplified loader
* relocation code (a little).
* 00000205 Bug fixes: a5 was be incorectly set the same as a4.
* Moved the app info block space from *a4 to *a5.
* 00000204 Bug fixes: unresolved references to global data.
* filename before <segment> in map file crasher.
* object file > segmentmaxsize crasher.
* 00000203 PalmOS 3.3 fix: open/close the library database in shlibcrt0.c.
* 00000202 Removed bogus exit when (a4 == 0) in crt0.c.
* 00000201 Changed LoadCodeSegments() so that ROMed code resources
* will not be unlocked in UnloadCodeSegments().
* 00000200 First versioned version.
* Fixed handling where there is only one code segment.
* Added -version option.
* Cleaned up Makefile.
*/
#define MULTILINK_VERSION_MAJOR ((MULTILINK_VERSION >> 16) & 0xff)
#define MULTILINK_VERSION_MINOR ((MULTILINK_VERSION >> 8) & 0xff)
#define MULTILINK_VERSION_MICRO ((MULTILINK_VERSION) & 0xff)
#ifndef MULTILINK_LIBDIR
#define MULTILINK_LIBDIR "/usr/local/m68k-palmos/lib/multilib"
#endif
/* crt variants */
#define LIBFILE_CRT0 "crt/crt0.o"
#define LIBFILE_GCRT0 "crt/gcrt0.o"
#define LIBFILE_SLCRT0 "crt/slcrt0.o"
#define LIBFILE_SLGCRT0 "crt/slgcrt0.o"
/* library files */
#define LIBFILE_DATA "lib/multilinkdata.o"
#define LIBFILE_LOAD "lib/multilinkload.o"
#define LIBFILE_GET "lib/multilinkget.o"
#define LIBFILE_RLOC "lib/multilinkrloc.o"
#define DEFAULT_AMX_PRIORITY 30 /* AMX task priority */
#define DEFAULT_HEAP_SIZE 4096 /* required heap space */
#define DEFAULT_STACK_SIZE 4096 /* required stack space */
#ifdef MULTILINK_A4_GLOBALS
#define DEFAULT_A4_GLOBALS 1
#else
#define DEFAULT_A4_GLOBALS 0
#endif
#define DEFAULT_AOUT_PREFIX "a"
#define DEFAULT_AOUT_SUFFIX ".out"
#ifdef MULTILINK_RELOCATION_OLD
#define DEFAULT_RELOCATION_OLD 1
#else
#define DEFAULT_RELOCATION_OLD 0
#endif
#define MEM_NEW(t) (t*)malloc(sizeof(t))
#define MEM_NEWZAP(t) (t*)calloc(1, sizeof(t))
#define MEM_CALLOC(n, s) calloc((n), (s))
#define MEM_MALLOC(n) malloc((n))
#define MEM_VECTOR(t, n) (t*)malloc(sizeof(t) * (n))
#define MEM_FREE(p) free(p)
typedef enum TargetType
{
TARGET_APP,
TARGET_SYSLIB
} TargetType;
/* lifted from obj-res.c */
static unsigned char *compress_data(unsigned char *raw,
long data_len,
long bss_len,
long *comp_len)
{
unsigned char *dp;
unsigned char *wp;
int block_len;
int count;
int total = 0;
wp = dp = malloc(data_len * 2 + 64); /* data could in theory grow a lot */
/* Thanks to Darrin Massena for this algorithm */
*(unsigned int *)wp = htonl(data_len);
wp += 4;
total += 4;
if ((data_len + bss_len) > 0x7ffc) {
fprintf(stderr, "error: .data and .bss too large for data #0 resource\n");
exit(1);
}
/* A5 offset */
*(unsigned int *)wp = htonl(-(((data_len + bss_len) + 3) & 0x7ffc));
wp += 4;
total += 4;
count = data_len;
while(count) {
block_len = (count < 0x80) ? count : 0x80;
*(wp++) = (block_len - 1) | 0x80;
total++;
memcpy(wp, raw, block_len);
wp += block_len;
raw += block_len;
total += block_len;
count -= block_len;
}
/* 3 separator bytes, and 2 more A5 offsets, all 0, = 11 more bytes */
memset(wp, 0, 11);
total += 11;
wp += 11; /* I think this should be here */
/* 6 longs of 0 for future compatibility with MW relocation extensions */
memset(wp, 0, 6*4);
total += 6*4;
*comp_len = total;
return dp;
}
typedef struct List
{
struct List* _next;
void* thing;
} List;
unsigned
ListGetSize(List* list)
{
unsigned n = 0;
while (list != NULL) {
n++;
list = list->_next;
}
return n;
}
void
ListInsert(List** list_a, List* newList, List* before)
{
List* l;
for (l = *list_a; l != NULL && l->_next != before; l = l->_next)
;
if (l != NULL) {
l->_next = newList;
newList->_next = before;
} else if (*list_a == before) {
*list_a = newList;
newList->_next = before;
} /* else we had a before but never found it */
}
void
ListAppend(List** list_a, List* newList)
{
ListInsert(list_a, newList, NULL);
}
void
ListPush(List** list_a, List* newList)
{
newList->_next = *list_a;
*list_a = newList;
}
List*
ListPop(List** list_a)
{
List* r;
if (*list_a != NULL) {
r = *list_a;
*list_a = r->_next;
} else {
r = NULL;
}
return r;
}
List*
ListFind(List* list, void* thing)
{
for (; list != NULL; list = list->_next) {
if (list->thing == thing)
return list;
}
return NULL;
}
static List* freeList;
void
ListFree(List* list)
{
ListPush(&freeList, list);
}
void
ListRemove(List** list_a, List* list) /* remove a list from chain */
{
List* l;
List* m;
for (l = *list_a, m = NULL; l != NULL; m = l, l = l->_next) {
if (l == list)
break;
}
if (l != NULL) {
if (m != NULL)
m->_next = l->_next;
else
*list_a = l->_next;
ListFree(l);
}
}
#define LIST_ALLOC_CHUNKCOUNT 64
List*
ListNew(void* thing)
{
List* l;
l = ListPop(&freeList);
if (l == NULL) {
int i;
l = MEM_VECTOR(List, LIST_ALLOC_CHUNKCOUNT);
for (i = 0; i < LIST_ALLOC_CHUNKCOUNT; i++) {
ListPush(&freeList, l);
l++;
}
l = ListPop(&freeList);
}
l->_next = NULL;
l->thing = thing;
return l;
}
typedef List SegmentList;
typedef List ObjectList;
typedef List SymbolList;
#define SegmentListNew(x) ListNew((Segment*)(x))
#define SegmentListGetSegment(x) (Segment*)(x)->thing
#define ObjectListNew(x) ListNew((Object*)(x))
#define ObjectListGetObject(x) (Object*)(x)->thing
#define SymbolListNew(x) ListNew((Symbol*)(x))
#define SymbolListGetSymbol(x) (Symbol*)(x)->thing
#define SEGMENT_HAS_STDLIB 0x1
#define STDLIB_SIZE (1024*8)
typedef struct Segment
{
ObjectList* objects;
SymbolList* jmpSymbols;
SymbolList* surrogateSymbols;
long dataBegin;
long dataEnd;
bfd_size_type textSize;
bfd_size_type bssSize;
bfd_size_type dataSize;
unsigned char* dataData;
int segmentNumber;
unsigned flags;
} Segment;
Segment*
SegmentNew()
{
Segment* newGuy = MEM_NEWZAP(Segment);
newGuy->segmentNumber = -1;
return newGuy;
}
#define OBJECT_IN_ARCHIVE 0x1
#define OBJECT_REFERENCED 0x2
typedef struct Object
{
Segment* segment;
ObjectList* users;
ObjectList* dependencies;
bfd_size_type textSize;
char* name;
SymbolList* symbols;
bfd* bfdData;
unsigned flags;
} Object;
Object*
ObjectNew(char* name, bfd* bfdData)
{
Object* newGuy = MEM_NEWZAP(Object);
newGuy->name = strdup(name);
newGuy->bfdData = bfdData;
return newGuy;
}
char*
ObjectGetName(Object* object)
{
return object->name;
}
#define BFD_IS_COMMON 0x20000
#define BFD_IS_DATA 0x40000
#define BFD_IS_UNDEFINED 0x80000
typedef struct Symbol
{
struct Symbol* _next;
char* name;
unsigned bfdFlags;
bfd_size_type size;
/* other stuff */
Object* object;
ObjectList* users; /* maybe wont use, see object */
} Symbol;
Symbol*
SymbolNew(char* name, flagword bfdFlags, Object* object)
{
Symbol* newGuy = MEM_NEWZAP(Symbol);
newGuy->name = strdup(name);
newGuy->bfdFlags = bfdFlags;
newGuy->object = object;
newGuy->_next = NULL;
newGuy->users = NULL;
return newGuy;
}
#define SymbolNewUndefined(n) SymbolNew((n), 0, NULL)
typedef struct SymbolTable
{
Symbol** heads;
unsigned size;
unsigned nentries;
} SymbolTable;
static unsigned long
hash_function(const void *xv)
{
unsigned long h = 0;
unsigned long g;
unsigned const char *x = (const char *) xv;
if (!x)
return 0;
while (*x != 0) {
h = (h << 4) + *x++;
if ((g = h & 0xf0000000) != 0)
h = (h ^ (g >> 24)) ^ g;
}
return h;
}
SymbolTable*
SymbolTableNew(unsigned p_size)
{
SymbolTable* table = MEM_NEW(SymbolTable);
unsigned size;
for (size = 0x1; size < (16*1024); size <<= 1) {
if (size >= p_size)
break;
}
table->size = size;
table->heads = (Symbol**)MEM_CALLOC(size, sizeof(Symbol*));
table->nentries = 0;
return table;
}
Symbol*
SymbolTableInsert(SymbolTable* table, Symbol* sym)
{
unsigned long hash = hash_function(sym->name);
unsigned long mask = table->size - 1;
unsigned index = (hash & mask);
sym->_next = table->heads[index];
table->heads[index] = sym;
table->nentries++;
return sym;
}
Symbol*
SymbolTableFind(SymbolTable* table, char* name)
{
Symbol* sym;
Symbol* head;
unsigned long hash = hash_function(name);
unsigned long mask = table->size - 1;
unsigned index = (hash & mask);
head = table->heads[index];
for (sym = head; sym != NULL; sym = sym->_next) {
if (strcmp(name, sym->name) == 0)
break;
}
return sym;
}
typedef int (*eh_dump_mappee_t)(Symbol* sym, void* arg);
static int
SymbolTableMap(SymbolTable* table, eh_dump_mappee_t func, void* arg)
{
Symbol* sym;
Symbol* head;
unsigned n;
for (n = 0; n < table->size; n++) {
head = table->heads[n];
for (sym = head; sym != NULL; sym = sym->_next) {
if ((func)(sym, arg) == -1)
return -1;
}
}
return 0;
}
typedef List LibdirList;
#define LibdirListNew(x) ListNew((Libdir*)(x))
#define LibdirListGetLibdir(x) (Libdir*)(x)->thing
#define LibraryListNew(x) ListNew((void*)(x))
#define LibraryListGetLibrary(x) (const char*)(x)->thing
static const char*
library_lookup(char* name, LibdirList* libdirList, const char* basename)
{
LibdirList* l;
const char* rv = NULL;
for (l = libdirList; l != NULL; l = l->_next) {
const char* dir = LibraryListGetLibrary(l);
int len = strlen(dir);
strcpy(name, dir);
if (name[len-1] != '/')
strcat(name, "/");
strcat(name, "lib");
strcat(name, basename);
strcat(name, ".a");
if (access(name, R_OK) == 0) {
rv = (const char*)name;
break;
}
}
return rv;
}
static void
library_add_paths(LibdirList** libdirList, const char* path)
{
const char* p = path;
for (;;) {
const char* q = strchr(p, ':');
char* s;
int len;
if (q != NULL)
len = q - p;
else
len = strlen(p);
s = (char*)malloc(len + 1);
memmove(s, p, len);
s[len] = '\0';
ListAppend(libdirList, LibraryListNew(s));
if (q != NULL)
p = q + 1;
else
break;
}
}
typedef struct LibName
{
char* basename;
char* libname;
} LibName;
typedef List LibNameList;
#define LibNameListNew(x) ListNew((LibName*)(x))
#define LibNameListGetLibName(x) (LibName*)(x)->thing
static LibNameList* theLibNameList;
static char* libDirVar;
static char* palmGcc;
static unsigned globalsInA4;
static unsigned relocateIs05;
static char*
get_library_filename(char* basename)
{
LibNameList* list;
LibName* lib;
for (list = theLibNameList; list != NULL; list = list->_next) {
lib = LibNameListGetLibName(list);
if (strcmp(lib->basename, basename) == 0) {
return lib->libname;
}
}
if (libDirVar == NULL) {
char* env = getenv("MULTILINK_LIBDIR");
if (env == NULL)
env = MULTILINK_LIBDIR;
libDirVar = strdup(env);
}
lib = MEM_NEWZAP(LibName);
lib->basename = strdup(basename);
lib->libname = (char*)MEM_MALLOC(strlen(libDirVar) +
strlen(basename) +
2);
strcpy(lib->libname, libDirVar);
strcat(lib->libname, "/");
strcat(lib->libname, basename);
list = LibNameListNew(lib);
ListPush(&theLibNameList, list);
return lib->libname;
}
/* The stuff that does something */
typedef struct {
SymbolTable* symbols;
SegmentList* segment;
ObjectList* objects;
} World;
static asymbol**
bfd_get_symbols(bfd* theBfd, unsigned* nSymbols_a)
{
long storage_needed;
asymbol** symbol_table;
long number_of_symbols;
storage_needed = bfd_get_symtab_upper_bound(theBfd);
if (storage_needed <= 0)
return NULL;
symbol_table = (asymbol **)MEM_MALLOC(storage_needed);
number_of_symbols = bfd_canonicalize_symtab(theBfd, symbol_table);
if (number_of_symbols < 0) {
MEM_FREE(symbol_table);
return NULL;
}
*nSymbols_a = number_of_symbols;
return symbol_table;
}
typedef struct EmitSegment
{
const char* filename;
bfd* bfd; /* the segment object */
asymbol** symbols; /* all the symbols in the segment */
unsigned nSymbols;
asection* data_section;
asection* reloc_section;
asection* bss_section;
asection* text_section;
} EmitSegment;
typedef struct Relocation
{
unsigned offset;
unsigned long adjustment;
} Relocation;
static Relocation*
RelocationNew(unsigned offset, unsigned long adjustment)
{
Relocation* relocation = MEM_NEW(Relocation);
relocation->adjustment = adjustment;
relocation->offset = offset;
return relocation;
}
#define RelocationListNew(x) ListNew((Relocation*)(x))
#define RelocationListGetRelocation(x) (Relocation*)(x)->thing
typedef struct EmitCtx
{
const char* basename;
EmitSegment* currentSegment;
List* relocations;
/* options */
unsigned long cid;
int verbosity;
int leavetmp;
int minusg;
TargetType targetType;
} EmitCtx;
static EmitSegment*
EmitSegmentInit(EmitSegment* segment, const char* filename)
{
bfd* bfd;
asymbol** symbols;
unsigned nsymbols;
bfd = bfd_openr(filename, NULL);
if (bfd_check_format(bfd, bfd_object) == 0) {
fprintf(stderr, "%s is not an object file\n", filename);
return NULL;
}
symbols = bfd_get_symbols(bfd, &nsymbols);
segment->filename = filename;
segment->bfd = bfd;
segment->symbols = symbols;
segment->nSymbols = nsymbols;
segment->data_section = bfd_get_section_by_name(bfd, ".data");
segment->reloc_section = bfd_get_section_by_name(bfd, ".reloc");
segment->bss_section = bfd_get_section_by_name(bfd, ".bss");
segment->text_section = bfd_get_section_by_name(bfd, ".text");
return segment;
}
static void
EmitSegmentFinalize(EmitSegment* segment)
{
bfd_close(segment->bfd);
}
static char*
make_resource_file_name(char* buf, const char* base,
const char* res_type, unsigned short res_id)
{
sprintf(buf, "%s%04x.", res_type, res_id);
if (base != NULL) {
strcat(buf, base);
strcat(buf, ".");
}
strcat(buf, "grc");
return buf;
}
static void
EmitResource(EmitCtx* eCtx,
const char* res_type, unsigned short res_id,
const void* buf, unsigned bufsize)
{
char filename[MAXPATHLEN];
FILE* fp;
make_resource_file_name(filename, eCtx->basename, res_type, res_id);
fp = fopen(filename, "wb");
fwrite(buf, 1, (size_t)bufsize, fp);
fclose(fp);
}
static EmitCtx*
EmitCtxInit(EmitCtx* eCtx,
const char* basename,
unsigned long cid,
int verbosity,
int leavetmp,
int minusg,
TargetType targetType)
{
eCtx->currentSegment = NULL;
eCtx->basename = basename;
eCtx->relocations = NULL;
/* options */
eCtx->cid = cid;
eCtx->verbosity = verbosity;
eCtx->leavetmp = leavetmp;
eCtx->minusg = minusg;
eCtx->targetType = targetType;
return eCtx;
}
static void
EmitCtxFinalize(EmitCtx* eCtx)
{
}
static int
bput_u32(bfd_byte** p_a, unsigned long value)
{
unsigned long buf = htonl(value);
bfd_byte* p = *p_a;
bfd_byte* q = (bfd_byte*)&buf;
*p++ = *q++;
*p++ = *q++;
*p++ = *q++;
*p++ = *q++;
*p_a = p;
return 4;
}
static int
bput_u16(bfd_byte** p_a, unsigned short value)
{
unsigned short buf = htons(value);
bfd_byte* p = *p_a;
bfd_byte* q = (bfd_byte*)&buf;
*p++ = *q++;
*p++ = *q++;
*p_a = p;
return 2;
}
#if 0
static int
bput_u8(bfd_byte** p_a, unsigned value)
{
unsigned char buf = value;
bfd_byte* p = *p_a;
*p++ = buf;
*p_a = p;
return 1;
}
#endif
static bfd_byte*
bfd_get_relocs_20(EmitCtx* eCtx, unsigned long* result_size_rv)
{
EmitSegment* segment = eCtx->currentSegment;
asection* data_section = segment->data_section;
asection* reloc_section = segment->reloc_section;
asection* bss_section = segment->bss_section;
asection* text_section = segment->text_section;
bfd* input_bfd = segment->bfd;
/* we only support relocations in the data section */
bfd_size_type reloc_size;
bfd_byte* reloc_vector;
bfd_byte* rel;
unsigned long num_pilot_relocs = 0;
bfd_byte* result;
bfd_byte* p;
*result_size_rv = 0;
if (!reloc_section)
return 0;
reloc_size = bfd_section_size(input_bfd, reloc_section);
if (reloc_size < 0)
return NULL;
reloc_vector = (bfd_byte*)MEM_MALLOC((size_t)reloc_size);
if (!bfd_get_section_contents(input_bfd,
reloc_section, reloc_vector,
0, reloc_size)) {
return NULL;
}
result = (bfd_byte*)MEM_MALLOC(2 /* short for nSymbols */ +
(((reloc_size/12)+1)*sizeof(pilot_reloc)));
p = result + 2;
for (rel = reloc_vector; rel < reloc_vector + reloc_size; rel += 12) {
unsigned int type;
unsigned long reloffset;
int relsecndx;
int symsecndx;
asection* sec;
asection* relsec;
asection* symsec;
pilot_reloc pr;
Relocation* relocation;
List* list;
unsigned long adjustment;
type = bfd_get_16(input_bfd, rel);
relsecndx = bfd_get_16(input_bfd, rel+2);
reloffset = bfd_get_32(input_bfd, rel+4);
symsecndx = bfd_get_16(input_bfd, rel+8);
if (type != 1) {
fprintf(stderr, "unknown reloc type 0x%x\n", type);
continue;
}
relsec = symsec = NULL;
for (sec = input_bfd->sections; sec; sec = sec->next) {
if (sec->index == relsecndx)
relsec = sec;
if (sec->index == symsecndx)
symsec = sec;
}
if (!relsec) {
fprintf(stderr, "reloc in non-data section\n");
continue;
}
if (!symsec) {
fprintf(stderr, "reloc relative to strange section\n");
continue;
}
pr.type = RELOC_ABS_32;
if (symsec == text_section) {
pr.section = TEXT_SECTION;
adjustment = text_section->vma;
} else if (symsec == data_section) {
pr.section = DATA_SECTION;
adjustment = data_section->vma;
} else if (symsec == bss_section) {
pr.section = BSS_SECTION;
adjustment = bss_section->vma;
} else {
fprintf(stderr, "reloc in bad section\n");
continue;
}
if (adjustment != 0) {
relocation = RelocationNew(reloffset, adjustment);
list = RelocationListNew(relocation);
ListPush(&eCtx->relocations, list);
}
#if 0
printf("offset = %ld\n", reloffset);
#endif
pr.offset = htons((unsigned short)reloffset);
/* We could less. The loader doesn't use it. */
pr.value = 0;
memcpy(p, &pr, 8);
p += 8;
num_pilot_relocs++;
}
MEM_FREE(reloc_vector);
*result_size_rv = p - result;
p = result;
bput_u16(&p, num_pilot_relocs);
return result;
}
static bfd_byte*
bfd_get_relocs_05(EmitCtx* eCtx, unsigned long* result_size_rv)
{
EmitSegment* segment = eCtx->currentSegment;
asection* data_section = segment->data_section;
asection* bss_section = segment->bss_section;