2 * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
3 * See the copyright notice in the ACK home directory, in the file "Copyright".
6 static char rcsid[] = "$Id: main.c,v 3.15 1996/11/21 10:14:26 ceriel Exp $";
10 * led - linkage editor for ACK assemblers output format
30 static initializations();
35 static struct outname *makename();
38 static norm_commons();
39 static complete_sections();
40 static change_names();
45 static do_statistics();
52 initializations(argc, argv);
55 if (statistics) do_statistics();
69 register struct memory *m = mems;
71 while (m <= &mems[NMEMS-1]) {
72 fprintf(stderr, "mem %d: full %lx, free %lx\n",
81 char *progname; /* Name this program was invoked with. */
82 int passnumber; /* Pass we are in. */
83 struct outhead outhead; /* Header of final output file. */
84 struct outsect outsect[MAXSECT];/* Its section table. */
88 initializations(argc, argv)
95 setbuf(stdin, (char *)NULL);
96 setbuf(stdout, (char *)NULL);
97 setbuf(stderr, (char *)NULL);
103 outhead.oh_magic = O_MAGIC;
104 outhead.oh_stamp = O_STAMP;
107 /* ------------------------ ROUTINES OF FIRST PASS ------------------------- */
109 int flagword = 0; /* To store command-line options. */
110 char *outputname = "a.out"; /* Name of the resulting object file. */
114 * Scan the arguments.
115 * If the argument starts with a '-', it's a flag, else it is either
116 * a plain file to be loaded, or an archive.
120 register char **argv;
126 extern char *strindex();
128 extern struct outname *searchname();
140 * The rest of the argument must be of the form
141 * `<section number>:<alignment>', where
142 * <section number> and <alignment> are numbers.
143 * <alignment> will be the alignment in the machine of
144 * section <section number>.
146 sectno = atoi(++argp);
147 if ((argp = strindex(argp, ':')) == (char *)0)
148 fatal("usage: -a<section number>:<alignment>");
149 setlign(sectno, number(++argp));
153 * The rest of the argument must be of the form
154 * `<section number>:<base>', where <section number>
155 * and base are decimal numbers. <base> will be
156 * the base address in the machine of section
159 sectno = atoi(++argp);
160 if ((argp = strindex(argp, ':')) == (char *)0)
161 fatal("usage: -b<section number>:<base>");
162 setbase(sectno, number(++argp));
166 * Leave relocation information in the output, so that
167 * a next pass can see where relocation was done. The
168 * resulting output however is no longer relocatable.
179 /* In the resulting name list, leave offsets with
180 respect to the beginning of the section instead
181 of absolute addresses.
188 * The `name' argument after -o is used as name
189 * of the led output file, instead of "a.out".
191 if ((outputname = *++argv) == (char *)0)
192 fatal("-o needs filename");
196 * Generate relocation information in the output file
197 * so that it can be the subject of another led run.
198 * This flag also prevents final definitions from being
199 * given to common symbols, and suppresses the
200 * `Undefined:' diagnostic.
202 if (flagword & CFLAG) break;
203 if (flagword & SFLAG)
204 warning("-r contradicts -s: -s ignored");
209 * `Strip' the output, that is, remove the symbol table
210 * and relocation table to save space (but impair the
211 * usefullness of the debuggers). This information can
212 * also be removed by astrip(1).
214 if (flagword & RFLAG)
215 warning("-s contradicts -r: -s ignored");
221 * Take the following argument as a symbol and enter it
222 * as undefined in the symbol table. This is useful for
223 * loading wholly from a library, since initially the
224 * symbol table is empty and an unresolved reference is
225 * needed to force the loading of the first routine.
227 if (*++argv == (char *)0)
228 fatal("-u needs symbol name");
230 if (searchname(*argv, h) == (struct outname *)0)
231 entername(makename(*argv), h);
240 warning("bad flag letter %c", *argp);
247 * If `s' starts with 0x/0X, it's hexadecimal,
248 * else if it starts with 0b/0B, it's binary,
249 * else if it starts with 0, it's octal,
257 register long value = 0;
258 register int radix = 10;
263 if (*s == 'x' || *s == 'X') {
266 } else if (*s == 'b' || *s == 'B') {
271 while (digit = *s++) {
272 if (digit >= 'A' && digit <= 'F')
273 digit = digit - 'A' + 10;
274 else if (digit >= 'a' && digit <= 'f')
275 digit = digit - 'a' + 10;
276 else if (digit >= '0' && digit <= '9')
279 fatal("wrong digit %c", digit);
281 fatal("digit %c exceeds radix %d", digit, radix);
282 value = radix * value + digit;
288 * We use one bit per section to indicate whether a base was already given or
289 * not. Only one base may be given. The same applies for alignments.
291 static char basemap[MAXSECT / WIDTH];
292 static long sect_base[MAXSECT];
293 static char lignmap[MAXSECT / WIDTH];
294 static long sect_lign[MAXSECT];
298 * Set the alignment of section `sectno' to `lign', if this doesn't
299 * conflict with earlier alignment.
302 setlign(sectno, lign)
306 extern bool setbit();
308 if (setbit(sectno, lignmap) && sect_lign[sectno] != lign)
309 fatal("section has different alignments");
311 fatal("alignment cannot be zero");
312 sect_lign[sectno] = lign;
316 * Set the base of section `sectno' to `base', if no other base has been
320 setbase(sectno, base)
324 extern bool setbit();
326 if (setbit(sectno, basemap) && sect_base[sectno] != base)
327 fatal("section has different bases");
328 sect_base[sectno] = base;
331 static struct outname *
335 static struct outname namebuf;
337 namebuf.on_mptr = string;
338 namebuf.on_type = S_UND + S_EXT;
339 namebuf.on_valu = (long)0;
345 * If `file' is a plain file, symboltable information and section sizes are
346 * extracted. If it is an archive it is examined to see if it defines any
353 if (getfile(file) == PLAIN) {
354 debug("%s: plain file\n", file, 0, 0, 0);
357 /* It must be an archive. */
358 debug("%s: archive\n", file, 0, 0, 0);
364 /* ---------------- ROUTINES BETWEEN FIRST AND SECOND PASS ----------------- */
367 * After pass 1 we know the sizes of all commons so we can give each common
368 * name an address within its section and we can compute the sizes of all
369 * sections in the machine. After this we can compute the bases of all
370 * sections. We then add the section bases to the values of names in
371 * corresponding sections.
378 if (!(flagword&(RFLAG|NFLAG)))
382 extern unsigned short NGlobals, NLocals;
385 * Sect_comm[N] is the number of common bytes in section N.
386 * It is computed after pass 1.
388 long sect_comm[MAXSECT];
391 * If there are undefined names, we print them and we set a flag so that
392 * the output can be subject to another led run and we return.
393 * We now know how much space each common name needs. We change the value
394 * of the common name from the size to the address within its section,
395 * just like "normal" names. We also count the total size of common names
396 * within each section to be able to compute the final size in the machine.
401 register struct outname *name;
403 register int und = FALSE;
405 name = (struct outname *)address(ALLOGLOB, (ind_t)0);
408 if (ISUNDEFINED(name)) {
411 if (!(flagword & RFLAG)) {
413 fprintf(stderr, "Undefined:\n");
415 outhead.oh_flags |= HF_LINK;
416 if (flagword & RFLAG) break;
417 flagword = (flagword & ~SFLAG) | RFLAG;
419 fprintf(stderr, "\t%s\n",
420 address(ALLOGCHR, (ind_t)name->on_foff)
425 if (flagword & RFLAG) return;
428 * RFLAG is off, so we need not produce relocatable output.
429 * We can now assign an address to common names.
430 * It also means that there are no undefined names.
432 name = (struct outname *)address(ALLOGLOB, (ind_t)0);
435 if (!ISABSOLUTE(name) && ISCOMMON(name)) {
437 register int sectindex;
439 size = name->on_valu; /* XXX rounding? */
440 sectindex = (name->on_type & S_TYP) - S_MIN;
442 outsect[sectindex].os_size +
443 sect_comm[sectindex];
444 sect_comm[sectindex] += size;
445 name->on_type &= ~S_COM;
451 struct orig relorig[MAXSECT];
454 * Compute the offsets in file and machine that the sections will have.
455 * Also set the origins to 0.
460 register long base = 0;
462 register struct outsect *sc;
463 register int sectindex;
465 foff = SZ_HEAD + outhead.oh_nsect * SZ_SECT;
466 for (sectindex = 0; sectindex < outhead.oh_nsect; sectindex++) {
467 relorig[sectindex].org_size = (long)0;
468 sc = &outsect[sectindex];
472 if (flagword & RFLAG)
475 sc->os_size += sect_comm[sectindex];
477 tstbit(sectindex, lignmap) ? sect_lign[sectindex] : 1;
478 if (tstbit(sectindex, basemap)) {
479 base = sect_base[sectindex];
480 if (sc->os_lign && base % sc->os_lign)
481 fatal("base not aligned");
482 } else if (sc->os_lign) {
483 base += sc->os_lign - 1;
484 base -= base % sc->os_lign;
492 * For each name we add the base of its section to its value, unless
493 * the output has to be able to be linked again, as indicated by RFLAG.
499 register struct outname *name;
501 name = (struct outname *)address(ALLOGLOB, (ind_t)0);
510 * Do the same with the local names.
512 name = (struct outname *)address(ALLOLOCL, (ind_t)0);
523 * This function sets a bit with index `indx' in string.
524 * It returns whether it was already set.
531 register int byte_index, bit_index;
534 byte_index = indx / WIDTH; /* Index of byte with bit we need. */
535 bit_index = indx % WIDTH; /* Index of bit we need. */
536 byte = string[byte_index];
538 if (byte & BIT) return TRUE;
542 string[byte_index] |= byte;
547 * This function returns whether the bit given by `indx' is set in `string'.
554 register int byte_index, bit_index;
557 byte_index = indx / WIDTH; /* Index of byte with bit we need. */
558 bit_index = indx % WIDTH; /* Index of bit we need. */
559 byte = string[byte_index];
566 * Add the base of the section of a name to its value.
569 struct outname *name;
571 register int type = name->on_type & S_TYP;
572 register int sectindex = type - S_MIN;
574 if (type == S_UND || type == S_ABS || type == S_CRS)
576 if (name->on_type & S_COM)
579 name->on_valu += outsect[sectindex].os_base;
580 debug( "%s: type 0x%x, value %ld\n",
581 address((name->on_type & S_EXT) ? ALLOGCHR : ALLOLCHR,
584 name->on_type, name->on_valu, 0
588 /* ------------------------ ROUTINES OF SECOND PASS ------------------------ */
591 * Flags have already been processed, so we ignore them here.
599 if ((*argv)[0] != '-') {
603 switch ((*argv)[1]) {
618 if (getfile(file) == PLAIN) {
619 debug("%s: plain file\n", file, 0, 0, 0);
622 /* It must be an archive. */
623 debug("%s: archive\n", file, 0, 0, 0);