forked from deplinenoise/vlink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
595 lines (524 loc) · 17.5 KB
/
main.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
/* $VER: vlink main.c V0.13 (02.11.10)
*
* This file is part of vlink, a portable linker for multiple
* object formats.
* Copyright (c) 1997-2010 Frank Wille
*
* vlink is freeware and part of the portable and retargetable ANSI C
* compiler vbcc, copyright (c) 1995-2010 by Volker Barthelmann.
* vlink may be freely redistributed as long as no modifications are
* made and nothing is charged for it. Non-commercial usage is allowed
* without any restrictions.
* EVERY PRODUCT OR PROGRAM DERIVED DIRECTLY FROM MY SOURCE MAY NOT BE
* SOLD COMMERCIALLY WITHOUT PERMISSION FROM THE AUTHOR.
*/
#define MAIN_C
#include "vlink.h"
struct GlobalVars gvars;
static char *get_option_arg(int argc,char *argv[],int *i)
/* get pointer to the string, which either directly follows the option
character or is stored in the next argument */
{
if (argv[*i][2])
return &argv[*i][2];
else {
if (++*i<argc) {
/* another option? */
if (argv[*i][0]!='-' || isdigit((unsigned char)argv[*i][1]))
return argv[*i];
}
--*i;
}
error(5,argv[*i][1]);
return NULL; /* not reached */
}
static char *get_arg(int argc, char *argv[],int *i)
{
if ((*i+1)<argc && *argv[*i+1]!='-')
return argv[++*i];
error(34,argv[*i]); /* option requires argument */
return NULL; /* not reached */
}
static void ReadListFile(struct GlobalVars *gv,char *name,uint16_t flags)
/* read a file, which contains a list of object file names */
{
FILE *f;
struct InputFile *ifn;
int c;
char buf[256],*n=NULL;
if (f = fopen(name,"r")) {
do {
int len = 0;
c = fgetc(f);
if (c == '\"') { /* read file name in quotes */
n = buf;
len = 0;
while ((c = fgetc(f))!=EOF && c!='\"') {
if (len < 255) {
len++;
*n++ = c;
}
}
}
else if (c > ' ') {
if (n == NULL) {
n = buf;
len = 0;
}
if (len < 255) {
len++;
*n++ = c;
}
}
if (c<=' ' && n) { /* file name ends here */
*n = 0;
ifn = alloc(sizeof(struct InputFile));
ifn->name = allocstring(buf);
ifn->lib = FALSE;
ifn->flags = flags;
addtail(&gv->inputlist,&ifn->n);
n = NULL;
}
}
while (c != EOF);
fclose(f);
}
else
error(8,name); /* cannot open */
}
static uint16_t chk_flags(char *s)
/* check for input-file flags */
{
if (!strcmp(s+5,"deluscore"))
return IFF_DELUNDERSCORE;
else if (!strcmp(s+5,"adduscore"))
return IFF_ADDUNDERSCORE;
error(2,s);
return 0;
}
static int flavours_cmp(const void *f1,const void *f2)
{
return strcmp(*(char **)f1,*(char **)f2);
}
void cleanup(struct GlobalVars *gv)
{
exit(gv->returncode);
}
int main(int argc,char *argv[])
{
struct GlobalVars *gv = &gvars;
int i,j;
char *buf;
struct LibPath *libp;
struct InputFile *ifn;
bool stdlib = TRUE;
int so_version = 0; /* minum version for shared objects */
uint16_t flags = 0; /* input file flags */
/* initialize and set default values */
memset(gv,0,sizeof(struct GlobalVars));
initlist(&gv->libpaths);
initlist(&gv->rpaths);
gv->dynamic = TRUE; /* link with dynamic libraries first */
gv->interp_path = DEFAULT_INTERP_PATH;
gv->soname = NULL;
gv->endianess = -1; /* endianess is unknown */
#ifdef DEFTARGET
for (j=0; fff[j]; j++) {
if (!strcmp(fff[j]->tname,DEFTARGET)) {
gv->dest_format = (uint8_t)j;
break;
}
}
if (fff[j] == NULL) {
fprintf(stderr,"Configuration warning: Selected default target "
"\"%s\" is not included.\nThe current default target "
"is \"%s\".\n",DEFTARGET,fff[gv->dest_format]->tname);
printf("\n");
}
#endif
initlist(&gv->inputlist);
initlist(&gv->lnksec);
gv->dest_name = "a.out";
gv->maxerrors = DEF_MAXERRORS;
gv->reloctab_format = RTAB_UNDEF;
if (argc<2 || (argc==2 && *argv[1]=='?')) {
show_usage();
exit(EXIT_SUCCESS);
}
for (i=1; i<argc; i++) {
if (*argv[i] == '-') { /* option detected */
switch (argv[i][1]) {
case 'b':
if (!strcmp(&argv[i][2],"aseoff")) { /* set base-relative offset */
long bo;
sscanf(get_arg(argc,argv,&i),"%li",&bo);
fff[gv->dest_format]->baseoff = bo;
}
else { /* select target format */
if (buf = get_option_arg(argc,argv,&i)) {
/* for compatibility with older vlink versions,
elf32amiga is automatically converted into elf32powerup
and amigaos into amigahunk */
if (!strcmp(buf,"elf32amiga"))
buf = "elf32powerup";
else if (!strcmp(buf,"amigaos"))
buf = "amigahunk";
for (j=0; fff[j]; j++) {
if (!strcmp(fff[j]->tname,buf))
break;
}
if (fff[j])
gv->dest_format = (uint8_t)j;
else
error(9,buf); /* invalid target format */
}
}
break;
case 'c':
if (!strncmp(&argv[i][2],"lr-",3))
flags &= ~(chk_flags(argv[i])); /* -clr-flags */
else
error(2,argv[i]); /* unrecognized option */
break;
case 'd':
gv->alloc_common = TRUE; /* force alloc. of common syms. */
break;
case 'e':
if (!strcmp(&argv[i][2],"xport-dynamic"))
gv->dyn_exp_all = TRUE; /* export all globals as dynamic */
else /* defines entry point */
gv->entry_name = get_option_arg(argc,argv,&i);
break;
case 'f':
if (!strcmp(&argv[i][2],"ixunnamed")) {
gv->fix_unnamed = TRUE; /* assign a name to unnamed sections */
}
else { /* set a flavour */
char *name,**fl;
if (name = get_option_arg(argc,argv,&i)) {
if (fl = gv->flavours.flavours) {
char **tmp = alloc((gv->flavours.n_flavours+1)*sizeof(char *));
memcpy(tmp,fl,gv->flavours.n_flavours*sizeof(char *));
free(fl);
fl = tmp;
}
else
fl = alloc(sizeof(char *));
fl[gv->flavours.n_flavours++] = name;
gv->flavours.flavours = fl;
gv->flavours.flavours_len += strlen(name) + 1;
}
}
break;
case 'h': /* help text */
show_usage();
exit(EXIT_SUCCESS);
case 'i':
if (!strcmp(&argv[i][2],"nterp"))
gv->interp_path = get_arg(argc,argv,&i);
else
error(2,argv[i]); /* unrecognized option */
break;
case 'l': /* library specifier */
if (buf = get_option_arg(argc,argv,&i)) {
ifn = alloc(sizeof(struct InputFile));
ifn->name = buf;
ifn->lib = TRUE;
ifn->dynamic = gv->dynamic;
ifn->so_ver = so_version;
so_version = 0;
ifn->flags = flags;
addtail(&gv->inputlist,&ifn->n);
}
break;
case 'm':
if (!strcmp(&argv[i][2],"inalign")) {
long a;
sscanf(get_arg(argc,argv,&i),"%li",&a);
gv->min_alignment = (uint8_t)a;
}
else if (!strcmp(&argv[i][2],"ultibase"))
gv->multibase = TRUE;
else
error(2,argv[i]); /* unrecognized option */
break;
case 'n':
if (!argv[i][2])
gv->no_page_align = TRUE;
else if (!strcmp(&argv[i][2],"ostdlib"))
stdlib = FALSE;
else
error(2,argv[i]); /* unrecognized option */
break;
case 'o': /* set output file name */
gv->dest_name = get_option_arg(argc,argv,&i);
break;
case 'q': /* force relocations into final executable */
gv->keep_relocs = TRUE;
break;
case 'r': /* output is an relocatable object again */
if (!argv[i][2]) {
gv->dest_object = TRUE;
}
else if (!strcmp(&argv[i][2],"path")) {
if (buf = get_arg(argc,argv,&i)) {
libp = alloc(sizeof(struct LibPath));
libp->path = buf;
addtail(&gv->rpaths,&libp->n);
}
}
else
error(2,argv[i]); /* unrecognized option */
break;
case 's':
switch (argv[i][2]) {
case '\0': /* strip all symbols */
gv->strip_symbols = STRIP_ALL;
break;
case 'c': /* -sc force small code */
gv->small_code = TRUE;
break;
case 'd': /* -d force small data */
gv->small_data = TRUE;
break;
case 'e':
if (!strncmp(&argv[i][3],"t-",2)) /* -set-flags */
flags |= chk_flags(argv[i]);
else
error(2,argv[i]); /* unrecognized option */
break;
case 'h': /* -shared */
if (!strcmp(&argv[i][3],"ared"))
gv->dest_sharedobj = TRUE;
else
error(2,argv[i]); /* unrecognized option */
break;
case 'o': /* -soname <real name> */
if (!strcmp(&argv[i][3],"name"))
gv->soname = get_arg(argc,argv,&i);
else
error(2,argv[i]); /* unrecognized option */
break;
case 't': /* -static */
if (!strcmp(&argv[i][3],"atic"))
gv->dynamic = FALSE;
else
error(2,argv[i]); /* unrecognized option */
break;
default:
error(2,argv[i]); /* unrecognized option */
break;
}
break;
case 't': /* trace file accesses */
if (!strcmp(&argv[i][2],"extbaserel"))
gv->textbaserel = TRUE;
else if (!strncmp(&argv[i][2],"os-",3)) {
/* -tos-options for targets ataritos and aoutmint */
if (!strcmp(&argv[i][5],"flags")) {
long fl;
sscanf(get_arg(argc,argv,&i),"%li",&fl);
gv->tosflags = fl;
}
else if (!strcmp(&argv[i][5],"fastload"))
gv->tosflags |= 1;
else if (!strcmp(&argv[i][5],"fastram"))
gv->tosflags |= 2;
else if (!strcmp(&argv[i][5],"fastalloc"))
gv->tosflags |= 4;
else if (!strcmp(&argv[i][5],"private"))
gv->tosflags &= ~0x30;
else if (!strcmp(&argv[i][5],"global"))
gv->tosflags |= 0x10;
else if (!strcmp(&argv[i][5],"super"))
gv->tosflags |= 0x20;
else if (!strcmp(&argv[i][5],"readable"))
gv->tosflags |= 0x30;
else
error(2,argv[i]); /* unrecognized option */
}
else if (!argv[i][2])
gv->trace_file = stderr;
else
error(2,argv[i]); /* unrecognized option */
break;
case 'u': /* mark symbol as undefined */
if (buf = get_option_arg(argc,argv,&i))
add_symnames(&gv->undef_syms,buf);
break;
case 'v': /* show version and target info */
show_version();
printf("Standard library path: "
#ifdef LIBPATH
LIBPATH
#endif
"\nDefault target: %s\n"
"Supported targets:",fff[gv->dest_format]->tname);
for (j=0; fff[j]; j++)
printf(" %s",fff[j]->tname);
printf("\n");
exit(EXIT_SUCCESS);
case 'w': /* suppress warnings */
gv->dontwarn = TRUE;
break;
case 'x': /* discard all local symbols */
gv->discard_local = DISLOC_ALL;
break;
case 'y': /* trace all accesses on a specific symbol */
if (gv->trace_syms == NULL)
gv->trace_syms = alloc_hashtable(TRSYMHTABSIZE);
if (buf = get_option_arg(argc,argv,&i)) {
struct SymNames **chain =
&gv->trace_syms[elf_hash(buf)%TRSYMHTABSIZE];
while (*chain)
chain = &(*chain)->next;
*chain = alloczero(sizeof(struct SymNames));
(*chain)->name = buf;
}
break;
case 'B': /* set link mode */
if (buf = get_option_arg(argc,argv,&i)) {
if (!strcmp(buf,"static")) {
gv->dynamic = FALSE;
}
else if (!strcmp(buf,"dynamic")) {
gv->dynamic = TRUE;
}
else if (!strcmp(buf,"shareable")) {
gv->dest_sharedobj = TRUE;
}
else if (!strcmp(buf,"forcearchive")) {
gv->whole_archive = TRUE;
}
else if (!strcmp(buf,"symbolic")) {
; /* don't know, what this means... */
}
else {
error(3,buf); /* unknown link mode */
}
}
break;
case 'C': /* select con-/destructor type */
if (buf = get_option_arg(argc,argv,&i)) {
if (!strcmp(buf,"gnu")) {
gv->collect_ctors_type = CCDT_GNU;
}
else if (!strcmp(buf,"vbcc")) {
gv->collect_ctors_type = CCDT_VBCC;
}
else if (!strcmp(buf,"vbccelf")) {
gv->collect_ctors_type = CCDT_VBCC_ELF;
}
else /* @@@ print error message */
gv->collect_ctors_type = CCDT_NONE;
}
break;
case 'E': /* set endianess */
switch (argv[i][2]) {
case 'B':
gv->endianess = _BIG_ENDIAN_;
break;
case 'L':
gv->endianess = _LITTLE_ENDIAN_;
break;
default:
error(2,argv[i]); /* unrecognized option */
break;
}
break;
case 'F': /* read a file with object file names */
if (buf = get_option_arg(argc,argv,&i))
ReadListFile(gv,buf,flags);
break;
case 'L': /* new library search path */
if (buf = get_option_arg(argc,argv,&i)) {
libp = alloc(sizeof(struct LibPath));
libp->path = buf;
addtail(&gv->libpaths,&libp->n);
}
break;
case 'M': /* mapping output */
gv->map_file = stdout;
break;
case 'P': /* protect symbol against stripping */
if (buf = get_option_arg(argc,argv,&i))
add_symnames(&gv->prot_syms,buf);
break;
case 'R': /* use short form for relocations */
if (buf = get_option_arg(argc,argv,&i)) {
if (!strcmp(buf,"std"))
gv->reloctab_format = RTAB_STANDARD;
else if (!strcmp(buf,"add"))
gv->reloctab_format = RTAB_ADDEND;
else if (!strcmp(buf,"short"))
gv->reloctab_format = RTAB_SHORTOFF;
else
error(123,buf); /* unknown reloc table format ignored */
}
break;
case 'S': /* strip debugger symbols */
gv->strip_symbols = STRIP_DEBUG;
break;
case 'T': /* read linker script file or set text address */
if (!strcmp(&argv[i][2],"text")) {
if (i+1 < argc)
sscanf(argv[++i],"%lli",&gv->start_addr);
else
error(5,'T'); /* option requires argument */
}
else if (buf = get_option_arg(argc,argv,&i)) {
if (gv->ldscript = mapfile(buf))
gv->scriptname = buf;
else
error(8,buf);
}
break;
case 'V': /* set minimum version for next shared object */
if (buf = get_option_arg(argc,argv,&i))
so_version = atoi(buf);
break;
case 'X': /* discard temporary local symbols only */
gv->discard_local = DISLOC_TMP;
break;
default:
error(2,argv[i]); /* unrecognized option */
break;
}
}
else { /* normal input file name */
ifn = alloc(sizeof(struct InputFile));
ifn->name = argv[i];
ifn->lib = FALSE;
ifn->flags = flags;
addtail(&gv->inputlist,&ifn->n);
}
}
/* add default library search path at the end of the list */
if (stdlib) {
#ifdef LIBPATH
libp = alloc(sizeof(struct LibPath));
libp->path = LIBPATH; /* default search path */
addtail(&gv->libpaths,&libp->n);
#endif
}
/* allocate flavour path buffer and sort flavours */
gv->flavours.flavour_dir = alloc(gv->flavours.flavours_len + 1);
if (gv->flavours.n_flavours > 1) {
qsort((void *)gv->flavours.flavours, gv->flavours.n_flavours,
sizeof(char **), flavours_cmp);
}
/* link them... */
linker_init(gv);
linker_load(gv); /* load all objects and libraries and their symbols */
linker_resolve(gv); /* resolve symbol references */
linker_relrefs(gv); /* find all relative references between sections */
linker_dynprep(gv); /* prepare for dynamic linking */
linker_join(gv); /* join sections with same name and type */
linker_mapfile(gv); /* mapfile output */
linker_copy(gv); /* copy section contents and fix symbol offsets */
linker_relocate(gv); /* relocate addresses in joined sections */
linker_write(gv); /* write output file in selected target format */
linker_cleanup(gv);
cleanup(gv);
return 0;
}