1#include <stdio.h>2#include <stdlib.h>3#include <string.h>45#include <scc/mach.h>67#include "libmach.h"89static int10matchseg(Map *map, Mapsec *msec, FILE *fp)11{12 unsigned long long o, e, mo, me;13 Section *seg;14 Mapsec *mseg, **v;1516 mo = msec->sec.load;17 me = mo + msec->sec.size;1819 if ((msec->sec.flags & SLOAD) == 0)20 return 0;2122 for (mseg = map->seg; mseg < &map->seg[map->nseg]; ++mseg) {23 if (mseg->fp != fp)24 continue;25 o = mseg->sec.load;26 e = o + mseg->sec.size;27 if (mo >= o && me <= e) {28 v = realloc(mseg->child, sizeof(Mapsec *) * (mseg->nchild+1));29 if (!v)30 return -1;31 mseg->child = v;32 v[mseg->nchild++] = msec;33 }34 }3536 return 0;37}3839int40mapsec(Map *map, Section *sec, FILE *fp, long fsiz)41{42 int n;43 char *s;44 Mapsec *msec, *mp;4546 mp = NULL;47 n = map->nsec;48 for (msec = map->sec; n--; msec++) {49 s = msec->sec.name;50 if (s && !strcmp(s, sec->name))51 goto found;52 if (!mp && !msec->used)53 mp = msec;54 }5556 if (mp) {57 msec = mp;58 goto found;59 }6061 if ((map = remap(map, map->nsec+1, map->nseg)) == NULL)62 return -1;63 msec = &map->sec[map->nsec-1];6465found:66 msec->used = 1;67 msec->sec = *sec;68 msec->fp = fp,69 msec->fsiz = fsiz;7071 return matchseg(map, msec, fp);72}