config: fix build with external compiler by passing the sysroot where needed
[AROS.git] / tools / collect-aros / backend-bfd.c
blob6221794a6b91ab0b9adbc2bba998ac89861ae303
1 /*
2 Copyright © 1995-2014, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #define PACKAGE
7 #define PACKAGE_VERSION
8 #include <bfd.h>
9 #include <string.h>
10 #include <stdlib.h>
11 #include <errno.h>
13 #include <sys/param.h>
15 #include "misc.h"
16 #include "backend.h"
18 static void bfd_fatal(const char *msg)
20 fatal(msg, bfd_errmsg(bfd_get_error()));
23 int check_and_print_undefined_symbols(const char *file)
25 bfd_byte *cur, *minisyms;
26 asymbol *store;
27 long symcount;
28 unsigned int size;
29 int there_are_undefined_syms = 0;
30 bfd *abfd;
32 bfd_init();
36 (abfd = bfd_openr(file, "default")) == NULL ||
37 !bfd_check_format(abfd, bfd_object)
40 bfd_fatal(file);
43 symcount = bfd_read_minisymbols(abfd, FALSE, (void **)&minisyms, &size);
44 if (symcount < 0)
45 bfd_fatal (bfd_get_filename (abfd));
47 if (symcount == 0)
48 return 0;
50 store = bfd_make_empty_symbol(abfd);
51 if (store == NULL)
52 bfd_fatal(bfd_get_filename(abfd));
54 for (cur = minisyms; cur < (minisyms + (symcount * size)); cur += size)
56 asymbol *sym;
58 sym = bfd_minisymbol_to_symbol(abfd, FALSE, (const void *)cur, store);
59 if (sym == NULL)
60 bfd_fatal(bfd_get_filename (abfd));
62 if (bfd_is_und_section (sym->section))
64 if (!there_are_undefined_syms)
66 there_are_undefined_syms = 1;
68 fprintf(stderr, "There are undefined symbols in '%s':\n", bfd_get_filename(abfd));
71 fprintf(stderr, "%s\n", sym->name);
75 bfd_close(abfd);
76 /* We should free() minisyms, but since we're called only once, we let the system
77 do it for us. */
79 return there_are_undefined_syms;
82 static void setfunc(bfd *exe, asection *sect, PTR setlist_ptr)
84 parse_secname(sect->name, (setnode **)setlist_ptr);
87 void collect_sets(const char *file, setnode **setlist_ptr)
89 bfd_init();
91 bfd *abfd;
95 (abfd = bfd_openr(file, "default")) == NULL ||
96 !bfd_check_format(abfd, bfd_object)
99 bfd_fatal(file);
102 parse_format(abfd->xvec->name);
103 bfd_map_over_sections(abfd, setfunc, setlist_ptr);
105 bfd_close(abfd);
108 static void collect_lib(asymbol *sym, setnode **liblist_ptr)
110 setnode *node;
111 char *cp, *name;
112 int pri;
114 if (strncmp(sym->name, "__aros_libreq_", 14) != 0)
115 return;
117 node = xmalloc(sizeof(*node)+strlen(sym->name)+1);
118 name = (char *)(&node[1]);
119 strcpy(name, sym->name);
121 cp = strchr(name + 14, '.');
122 if (cp != NULL) {
123 char *tmp;
124 pri = strtoul(cp+1, &tmp, 0);
126 if ((cp+1) == tmp) {
127 free(node);
128 return;
131 *(cp++) = 0;
133 } else {
134 pri = 0;
137 node->secname = name;
138 node->off_setname = 14;
139 node->pri = pri;
140 node->next = *liblist_ptr;
141 *liblist_ptr = node;
144 void collect_libs(const char *file, setnode **liblist_ptr)
146 long symtab_size;
147 bfd *abfd;
149 /* We assume bfd_init() has already been colled by
150 * collect_sets
155 (abfd = bfd_openr(file, "default")) == NULL ||
156 !bfd_check_format(abfd, bfd_object)
159 bfd_fatal(file);
162 symtab_size = bfd_get_symtab_upper_bound(abfd);
163 if (symtab_size > 0) {
164 asymbol **symtab;
165 long symbols;
167 symtab = (asymbol **)xmalloc(symtab_size);
168 symbols = bfd_canonicalize_symtab(abfd, symtab);
169 if (symbols > 0) {
170 long i;
172 for (i = 0; i < symbols; i++)
173 collect_lib(symtab[i], liblist_ptr);
176 free(symtab);
179 bfd_close(abfd);