summaryrefslogtreecommitdiffstats
path: root/tools/apriori/prelinkmap.c
blob: 739c181767cffcd3c9d21351d7f27631f5ea9741 (plain)
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
#include <prelinkmap.h>
#include <debug.h>
#include <errno.h>
#include <string.h>
#include <libgen.h>
#include <ctype.h>

typedef struct mapentry mapentry;

struct mapentry
{
    mapentry *next;
    unsigned base;
    char name[0];
};

static mapentry *maplist = 0;

/* These values limit the address range within which we prelinked libraries
   reside.  The limit is not set in stone, but should be observed in the 
   prelink map, or the prelink step will fail.
*/

#define PRELINK_MIN 0x90000000
#define PRELINK_MAX 0xB0000000

void pm_init(const char *file)
{
    unsigned line = 0;
    char buf[256];
    char *x;
    unsigned n;
    FILE *fp;
    mapentry *me;
    unsigned last = -1UL;
    
    fp = fopen(file, "r");
    FAILIF(fp == NULL, "Error opening file %s: %s (%d)\n", 
           file, strerror(errno), errno);

    while(fgets(buf, 256, fp)){
        x = buf;
        line++;
        
        /* eat leading whitespace */
        while(isspace(*x)) x++;

        /* comment or blank line? skip! */
        if(*x == '#') continue;
        if(*x == 0) continue;

        /* skip name */
        while(*x && !isspace(*x)) x++;

        if(*x) {
            *x++ = 0;
            /* skip space before address */
            while(*x && isspace(*x)) x++;
        }

        /* no address? complain. */
        if(*x == 0) {
            fprintf(stderr,"warning: %s:%d no base address specified\n",
                    file, line);
            continue;
        }
        
        n = strtoul(x, 0, 16);
        /* Note that this is not the only bounds check.  If a library's size
           exceeds its slot as defined in the prelink map, the prelinker will
           exit with an error.  See pm_report_library_size_in_memory().
        */
        FAILIF((n < PRELINK_MIN) || (n > PRELINK_MAX),
               "%s:%d base 0x%08x out of range.\n",
               file, line, n);
        
        me = malloc(sizeof(mapentry) + strlen(buf) + 1);
        FAILIF(me == NULL, "Out of memory parsing %s\n", file);

        FAILIF(last <= n, "The prelink map is not in descending order "
               "at entry %s (%08x)!\n", buf, n);
        last = n;
        
        me->base = n;
        strcpy(me->name, buf);
        me->next = maplist;
        maplist = me;        
    }

    fclose(fp);
}

/* apriori() calls this function when it determine the size of a library 
   in memory.  pm_report_library_size_in_memory() makes sure that the library
   fits in the slot provided by the prelink map.
*/
void pm_report_library_size_in_memory(const char *name,
                                      off_t fsize)
{
    char *x;
    mapentry *me;
    
    x = strrchr(name,'/');
    if(x) name = x+1;

    for(me = maplist; me; me = me->next){
        if(!strcmp(name, me->name)) {
            off_t slot = me->next ? me->next->base : PRELINK_MAX;
            slot -= me->base;
            FAILIF(fsize > slot,
                   "prelink map error: library %s@0x%08x is too big "
                   "at %lld bytes, it runs %lld bytes into "
                   "library %s@0x%08x!\n",
                   me->name, me->base, fsize, fsize - slot,
                   me->next->name, me->next->base);
            break;
        }
    }
    
    FAILIF(!me,"library '%s' not in prelink map\n", name);
}

unsigned pm_get_next_link_address(const char *lookup_name)
{
    char *x;
    mapentry *me;
    
    x = strrchr(lookup_name,'/');
    if(x) lookup_name = x+1;
    
    for(me = maplist; me; me = me->next){
        if(!strcmp(lookup_name, me->name)) {
            return me->base;
        }
    }
    
    FAILIF(1==1,"library '%s' not in prelink map\n", lookup_name);
    return 0;
}