~ubuntu-branches/ubuntu/quantal/gclcvs/quantal

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
/*
    File rsym_macosx.c
    
    Build an executable rsym for Mac OS X (31 July 2003).
    
    Grab only the external symbols from a Mach-O object file, and put them
    in a simple format. This information will be used for relocation.
    
    To compile in standalone mode:
        gcc -DDEBUG -I../h -o rsym_macosx rsym_macosx.c
    
    Aurelien Chanudet <aurelien.chanudet(at)m4x.org>
*/

#include <mach-o/loader.h>
#include <mach-o/nlist.h>

#include <mach/mach.h>

#ifndef SPECIAL_RSYM
#define SPECIAL_RSYM
#endif

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

#define IN_RSYM 1

/* #include "config.h" */
#include "ext_sym.h"

int verbose = 0;

void rsym_error (char *format, ...)
{
    va_list ap;
    
    va_start (ap, format);
    fprintf (stderr, "rsym: ");
    vfprintf (stderr, format, ap);
 /* fprintf (stderr, " (%s)", strerror(errno)); */
    fprintf (stderr, "\n");
    va_end (ap);
    exit (1);
}

int rsym_select_symbol(struct nlist *symbol)
{
    if (symbol->n_type & N_STAB) {
        return (FALSE);
    }
    if (!(symbol->n_type & N_EXT)) {
        return (FALSE);
    }
 /* if (symbol->n_type == N_UNDF) {
        return (FALSE);
    }
    if (symbol->n_sect == NO_SECT) {
        return (FALSE);
    }
    */
    return (TRUE);
}

void rsym_doit2 (struct nlist *symbols, unsigned long nsyms, char *outfile)
{
    unsigned long i;
    struct lsymbol_table tab;
    FILE *symout;
    
    if (!(symout = fopen (outfile, "wb"))) {
        rsym_error ("cannot open file for writing: %s", outfile);
    }

    tab.n_symbols=0;
    tab.tot_leng=0;
    
    fseek (symout, sizeof(tab), 0);
    
    for (i=0 ; i < nsyms ; i++)
    {
        int addr = (int) symbols[i].n_value;
        char *name = symbols[i].n_un.n_name;
        
        if (name)
        {
            tab.n_symbols++;
            fwrite (&addr, sizeof (int), 1, symout);
            while (tab.tot_leng++, *name)
                putc (*name++, symout);
            putc (0, symout);
        }
        else {
            fprintf (stderr, "warning: malformed symbol\n");
        }
    }
    
    if (verbose) {
        fprintf (stdout, "%d/%ld symbol(s) reviewed\n", tab.n_symbols, nsyms);
    }
    
    fseek (symout, 0, 0);
    fwrite (&tab, sizeof(tab), 1, symout);
    
    fclose (symout);
}

void rsym_doit1 (char *infile, char *outfile)
{
    struct stat stat_buf;
    struct mach_header *mh;
    struct load_command *lc;
    struct symtab_command *st = NULL;
    struct dysymtab_command *dyst;
    unsigned long nsyms, i, size;
    unsigned long strsize;
    struct nlist *symtab, *selected_symbols;
    kern_return_t r;
    char *strtab;
    vm_size_t s;
    char *addr;
    int fd;
    
    if ((fd = open (infile, O_RDONLY, 0)) < 0) {
        rsym_error ("cannot open input file: %s", infile);
    }
    
    if (fstat (fd, &stat_buf) == -1) {
        rsym_error ("cannot fstat file: %s", infile);
    }
    
    size = stat_buf.st_size;
    
    if ((r = map_fd (fd, (vm_offset_t) 0, (vm_offset_t *) &addr,
            (boolean_t) TRUE, (vm_size_t) size)) != KERN_SUCCESS) {
        rsym_error ("cannot map file in memory: %s", infile);
    }
    
    mh = (struct mach_header *) addr;
    lc = (struct load_command *) ((char *) addr + sizeof(struct mach_header));
    
    for (i=0 ; i < mh->ncmds ; i++) {
        if (lc->cmd == LC_SYMTAB) {
            st = (struct symtab_command *) lc;
        }
        else if (lc->cmd == LC_DYSYMTAB) {
            dyst = (struct dysymtab_command *) lc;
        }
        lc = (struct load_command *) ((char *) lc + lc->cmdsize);
    }
    
    if (!st) {
        rsym_error ("no symbol table information");
    }

    symtab = (struct nlist *) ((char *) addr + st->symoff);
    
    s = sizeof(struct nlist) * st->nsyms;
    if (vm_allocate (mach_task_self (),
		     (vm_address_t *) &selected_symbols, s, 1) != KERN_SUCCESS) {
        rsym_error ("could not vm_allocate");
    }
    
    nsyms = 0;
    
    for (i = 0 ; i < st->nsyms ; i++) {
        if (rsym_select_symbol (symtab + i))
            selected_symbols[nsyms++] = symtab[i];
    }
    
    strtab = addr + st->stroff;
    strsize = st->strsize;
    
    for (i = 0 ; i < nsyms ; i++) {
        if (selected_symbols[i].n_un.n_strx == 0)
            selected_symbols[i].n_un.n_name = "";
        else if (selected_symbols[i].n_un.n_strx < 0 ||
                selected_symbols[i].n_un.n_strx > strsize)
            selected_symbols[i].n_un.n_name = "bad string index";
        else
            selected_symbols[i].n_un.n_name =
	      selected_symbols[i].n_un.n_strx + strtab;
        
      	if (verbose) {
            fprintf (stdout, "%8x %s\n",
		   (unsigned int) selected_symbols[i].n_value,
		   selected_symbols[i].n_un.n_name);
        }
    }
    
    rsym_doit2 (selected_symbols, nsyms, outfile);
    
    if (vm_deallocate (mach_task_self (),
		       (vm_address_t) selected_symbols, s) != KERN_SUCCESS) {
        fprintf (stderr, "warning: failed to free memory\n");
    }
    
    if (vm_deallocate (mach_task_self (),
		       (vm_address_t) addr, (vm_size_t) size) != KERN_SUCCESS) {
        fprintf (stderr, "warning: failed to deallocate mapped file\n");
    }
        
    close (fd);
}

int main (int argc, char **argv, char **envp)
{
    int ch;
        
    while ((ch = getopt (argc, argv, "-v")) != -1) {
        if (ch == 'v')
            verbose = 1;
    }
    
    argc -= optind;
    argv += optind;
    
    if (argc < 2) {
        fprintf (stderr, "usage: rsym [-v(erbose)] <infile> <outfile>\n");
        exit (1);
    }

    rsym_doit1 (argv[0], argv[1]);
    
    return 0;
}