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: scan.c,v 3.19 1994/06/24 10:35:17 ceriel Exp $";
10 #include <sys/types.h>
24 #define IND_EMIT(x) (IND_CHAR(x) + (ind_t)align((x).oh_nchar))
25 #define IND_RELO(x) (IND_EMIT(x) + (x).oh_nsect * sizeof(ind_t))
27 #define IND_DBUG(x) (IND_RELO(x) + sizeof(ind_t))
31 extern char *core_alloc();
34 extern int passnumber;
36 char *archname; /* Name of archive, if reading from archive. */
37 char *modulname; /* Name of object module. */
43 static char *modulbase;
44 static long modulsize();
46 static bool all_alloc();
47 static bool direct_alloc();
48 static bool indirect_alloc();
49 static bool putemitindex();
50 static bool putreloindex();
52 static bool putdbugindex();
54 static get_indirect();
58 * Open the file with name `filename' (if necessary) and examine the first
59 * few bytes to see if it's a plain file or an archive.
60 * In case of a plain file, the file pointer is repositioned after the
61 * examination. Otherwise it is at the beginning of the table of contents.
67 unsigned int rd_unsigned2();
68 struct ar_hdr archive_header;
69 unsigned short magic_number;
76 modulname = (char *)0;
78 if (passnumber == FIRST || !incore) {
79 if ((infile = open(filename, READ)) < 0)
80 fatal("can't read %s", filename);
81 magic_number = rd_unsigned2(infile);
83 modulbase = modulptr((ind_t)0);
84 magic_number = *(unsigned short *)modulbase;
87 switch (magic_number) {
90 if (passnumber == FIRST || !incore) {
91 if (fstat(infile, &statbuf) < 0)
92 fatal("cannot stat %s", filename);
93 objectsize = statbuf.st_size;
102 if (passnumber == FIRST) {
103 rd_arhdr(infile, &archive_header);
104 if (strcmp(archive_header.ar_name, SYMDEF))
105 fatal("%s: no table of contents", filename);
107 modulbase += sizeof(int);
108 core_position += sizeof(int);
112 fatal("%s: wrong magic number", filename);
121 if (passnumber == FIRST || !incore)
125 get_archive_header(archive_header)
126 register struct ar_hdr *archive_header;
128 if (passnumber == FIRST || !incore) {
129 rd_arhdr(infile, archive_header);
132 *archive_header = *(struct ar_hdr *)modulbase;
133 modulbase += int_align(sizeof(struct ar_hdr));
134 core_position += int_align(sizeof(struct ar_hdr));
137 objectsize = archive_header.ar_size;
143 if (passnumber == FIRST) {
146 } else if (!incore) {
153 * Read module from the current file. If it doesn't fit into core, the strategy
154 * to keep everything in core is abandoned, but we will always put the header,
155 * the section table, and the name and string table into core.
161 struct outhead *head;
162 struct outsect *sect;
165 head = (struct outhead *)modulptr(IND_HEAD);
167 sect = (struct outsect *)modulptr(IND_SECT(*head));
168 get_indirect(head, sect);
170 rd_name((struct outname *)modulptr(IND_NAME(*head)), head->oh_nname);
171 rd_string((char *)modulptr(IND_CHAR(*head)), head->oh_nchar);
174 get_dbug(*(ind_t *)modulptr(IND_DBUG(*head)),
175 ojectsize - OFF_DBUG(*head)
182 * Allocate space for and read in the header and section table.
183 * First get the header. With this we can determine what to allocate
184 * for the rest of the module, and with the rest we can determine what
185 * to allocate for the section contents.
186 * If possible, allocate space for the rest of the module. Return whether
193 extern ind_t hard_alloc();
195 if (hard_alloc(ALLOMODL, (long)sizeof(struct outhead)) == BADOFF)
196 fatal("no space for module header");
197 rd_ohead((struct outhead *)modulptr(IND_HEAD));
199 * Copy the header because we need it so often.
201 head = *(struct outhead *)modulptr(IND_HEAD);
202 return direct_alloc(&head) && indirect_alloc(&head);
206 * Allocate space for the rest of the direct bytes.
207 * First allocate the section table and read it in, then allocate the rest
208 * and return whether this succeeded.
212 struct outhead *head;
214 ind_t sectindex = IND_SECT(*head);
215 register struct outsect *sects;
216 unsigned short nsect = head->oh_nsect;
218 extern ind_t hard_alloc();
219 extern ind_t alloc();
222 rest = nsect * sizeof(ind_t) + sizeof(ind_t) + sizeof(ind_t);
224 rest = nsect * sizeof(ind_t) + sizeof(ind_t);
227 * We already allocated space for the header, we now need
228 * the section, name an string table.
230 size = modulsize(head) - sizeof(struct outhead) - rest;
231 if (hard_alloc(ALLOMODL, size) == BADOFF)
232 fatal("no space for module");
233 rd_sect(sects = ((struct outsect *)modulptr(sectindex)), nsect);
235 if (sects->os_lign > 1) {
236 sects->os_size += sects->os_lign - 1;
237 sects->os_size -= sects->os_size % sects->os_lign;
242 return incore && alloc(ALLOMODL, rest) != BADOFF;
246 * Allocate space for the indirectly accessed pieces: the section contents and
247 * the relocation table, and put their indices in the right place.
251 struct outhead *head;
253 register int allopiece;
254 unsigned short nsect = head->oh_nsect;
255 unsigned short nrelo = head->oh_nrelo;
256 ind_t sectindex = IND_SECT(*head);
257 ind_t emitoff = IND_EMIT(*head);
258 ind_t relooff = IND_RELO(*head);
260 ind_t dbugoff = IND_DBUG(*head);
261 extern long objectsize;
262 long dbugsize = objectsize - OFF_DBUG(*head);
266 for (allopiece = ALLOEMIT; allopiece < ALLOEMIT + nsect; allopiece++) {
267 if (!putemitindex(sectindex, emitoff, allopiece))
269 sectindex += sizeof(struct outsect);
270 emitoff += sizeof(ind_t);
273 return putreloindex(relooff, (long)nrelo * sizeof(struct outrelo))
275 putdbugindex(dbugoff, dbugsize);
277 return putreloindex(relooff, (long)nrelo * sizeof(struct outrelo));
282 * Allocate space for the contents of the section of which the table entry is
283 * at offset `sectindex'. Put the offset of the allocated piece at offset
287 putemitindex(sectindex, emitoff, allopiece)
294 extern ind_t alloc();
295 static long zeros[MAXSECT];
296 register long zero = zeros[allopiece - ALLOEMIT];
299 * Notice that "sectindex" is not a section number!
300 * It contains the offset of the section from the beginning
301 * of the module. Thus, it cannot be used to index "zeros".
305 flen = ((struct outsect *)modulptr(sectindex))->os_flen;
307 if ((emitindex = alloc(allopiece, zero)) != BADOFF){
308 register char *p = address(allopiece, emitindex);
310 debug("Zeros %ld\n", zero, 0,0,0);
311 while (zero--) *p++ = 0;
316 zeros[allopiece - ALLOEMIT] =
317 zero + ((struct outsect *) modulptr(sectindex))->os_size - flen;
318 if ((emitindex = alloc(allopiece, flen)) != BADOFF) {
319 *(ind_t *)modulptr(emitoff) = emitindex;
326 * Allocate space for a relocation table with `nrelobytes' bytes, and put the
327 * offset at `relooff'.
330 putreloindex(relooff, nrelobytes)
335 extern ind_t alloc();
337 if ((reloindex = alloc(ALLORELO, nrelobytes)) != BADOFF) {
338 *(ind_t *)modulptr(relooff) = reloindex;
345 * Allocate space for debugging information and put the offset at `dbugoff'.
348 putdbugindex(dbugoff, ndbugbytes)
353 extern ind_t alloc();
355 if ((dbugindex = alloc(ALLODBUG, ndbugbytes)) != BADOFF) {
356 *(ind_t *)modulptr(dbugoff) = dbugindex;
364 * Compute addresses and read in. Remember that the contents of the sections
365 * and also the relocation table are accessed indirectly.
368 get_indirect(head, sect)
369 struct outhead *head; /* not register! Won't compile on
370 SCO Xenix 386 if it is!
372 register struct outsect *sect;
374 register ind_t *emitindex;
379 emitindex = (ind_t *)modulptr(IND_EMIT(*head));
381 for (nsect = 0; nsect < head->oh_nsect; nsect++) {
383 rd_emit(address(piece, *emitindex), sect->os_flen);
384 piece++; emitindex++; sect++;
386 reloindex = (ind_t *)modulptr(IND_RELO(*head));
387 rd_relo((struct outrelo *)address(ALLORELO, *reloindex),
393 * Set the file pointer at `pos'.
398 if (passnumber == FIRST || !incore)
399 lseek(infile, pos, 0);
403 * A file pointer is advanced automatically when reading, a char pointer
404 * is not. That's why we do it here. If we don't keep everything in core,
405 * we give the space allocated for a module back.
408 struct outhead *head;
410 register ind_t skip = modulsize(head);
413 core_position += int_align(skip);
414 if (passnumber == SECOND)
415 modulbase += int_align(skip);
418 core_position = (ind_t)0;
423 * Read in what we need in pass 2, because we couldn't keep it in core.
428 struct outhead *head;
429 register struct outsect *sects;
430 struct outname *names;
432 ind_t sectindex, nameindex, charindex;
433 unsigned short nsect, nname;
436 extern ind_t hard_alloc();
438 assert(passnumber == SECOND);
440 if (hard_alloc(ALLOMODL, (long)sizeof(struct outhead)) == BADOFF)
441 fatal("no space for module header");
442 head = (struct outhead *)modulptr(IND_HEAD);
444 nsect = head->oh_nsect; sectindex = IND_SECT(*head);
445 nname = head->oh_nname; nameindex = IND_NAME(*head);
446 nchar = head->oh_nchar; charindex = IND_CHAR(*head);
448 size = modulsize(head) - (nsect * sizeof(ind_t) + 2 * sizeof(ind_t));
450 size = modulsize(head) - (nsect * sizeof(ind_t) + sizeof(ind_t));
452 if (hard_alloc(ALLOMODL, size) == BADOFF)
453 fatal("no space for module");
455 sects = (struct outsect *)modulptr(sectindex);
456 names = (struct outname *)modulptr(nameindex);
457 chars = modulptr(charindex);
459 rd_sect(sects, nsect);
461 if (sects->os_lign > 1) {
462 sects->os_size += sects->os_lign - 1;
463 sects->os_size -= sects->os_size % sects->os_lign;
467 rd_name(names, nname);
468 rd_string(chars, nchar);
472 * Align `size' to a multiple of the size of a double.
473 * This is assumed to be a power of 2.
479 return (size + (sizeof(double) - 1)) & ~(int)(sizeof(double) - 1);
483 * Compute how many DIRECT bytes must be allocated for a module of which the
484 * header is pointed to by `head':
486 * 1. the section table,
488 * 3. the string table,
489 * 4. for each section the offset of its contents,
490 * 5. the offset of the relocation table.
492 * 6. the offset of the debugging information.
497 register struct outhead *head;
499 return sizeof(struct outhead) + /* 0 */
500 head->oh_nsect * sizeof(struct outsect) + /* 1 */
501 head->oh_nname * sizeof(struct outname) + /* 2 */
502 align(head->oh_nchar) + /* 3 */
503 head->oh_nsect * sizeof(ind_t) + /* 4 */
505 sizeof(ind_t) + /* 5 */
506 sizeof(ind_t); /* 6 */
508 sizeof(ind_t); /* 5 */
512 /* ------------------------------------------------------------------------- */
515 * Walk through the relocation table of the current module. We must either walk
516 * through core or through file. Startrelo() should be called first.
519 static struct outrelo *walkrelo;
520 static unsigned short cnt_relos;
521 static unsigned short relind;
525 register struct outhead *head;
530 reloindex = *(ind_t *)(modulbase + IND_RELO(*head));
531 walkrelo = (struct outrelo *)address(ALLORELO, reloindex);
536 cnt_relos = head->oh_nrelo;
543 static struct outrelo relobuf[_RELSIZ];
548 if (relind == _RELSIZ) {
549 unsigned int i = cnt_relos >= _RELSIZ ? _RELSIZ : cnt_relos;
555 return &relobuf[relind++];
558 /* ------------------------------------------------------------------------- */
561 * Get the section contents in core of which the describing struct has index
562 * `sectindex'. `Head' points to the header of the module.
565 getemit(head, sects, sectindex)
566 struct outhead *head;
567 struct outsect *sects;
572 extern char *core_alloc();
575 ret = core_alloc(ALLOMODL, sects[sectindex].os_flen);
576 if (ret == (char *)0)
578 rd_outsect(sectindex);
579 rd_emit(ret, sects[sectindex].os_flen);
583 * We have an offset in the contents of the final output
584 * "file" where normally the contents would be.
586 off = *((ind_t *)(modulbase + IND_EMIT(*head)) + sectindex);
587 return address(ALLOEMIT + sectindex, off);
591 getblk(totalsz, pblksz, sectindex)
597 long sz = (1L << 30);
601 while (sz >= totalsz) sz >>= 1;
603 ret = core_alloc(ALLOMODL, sz);
604 if (ret != (char *) 0) {
605 rd_outsect(sectindex);
611 fatal("no space for section contents");
618 core_free(ALLOMODL, emit);