grub2: bring back build of aros-side grub2 tools
[AROS.git] / tools / collect-aros / backend-generic.c
blobc9f20a67d8b8858abcebf75af791a8191b8856b3
1 /*
2 Copyright © 1995-2014, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include "misc.h"
7 #include "backend.h"
8 #include <sys/param.h>
9 #include <string.h>
10 #include <errno.h>
11 #include <stdlib.h>
13 #include "env.h"
15 static FILE *my_popen(const char *command, const char *file)
17 static char command_buf[MAXPATHLEN];
19 size_t command_len = strlen(command);
20 size_t file_len = strlen(file);
22 FILE *pipe;
24 if (file_len + command_len >= sizeof(command_buf))
25 fatal("collect_sets()", strerror(ENAMETOOLONG));
27 memcpy(command_buf, command, command_len);
28 memcpy(command_buf + command_len, file, file_len + 1);
30 set_compiler_path();
32 pipe = popen(command_buf, "r");
33 if (pipe == NULL)
34 fatal(command_buf, strerror(errno));
36 return pipe;
41 This routine is slow, but does the work and it's the simplest to write down.
42 All this will get integrated into the linker anyway, so there's no point
43 in doing optimizations
45 void collect_sets(const char *file, setnode **setlist_ptr)
47 char secname[201];
49 FILE *pipe = my_popen(OBJDUMP_NAME " -h ", file);
51 /* This fscanf() simply splits the whole stream into separate words */
52 while (fscanf(pipe, " %200s ", secname) > 0)
54 parse_format(secname);
55 parse_secname(secname, setlist_ptr);
58 pclose(pipe);
62 This routine is slow, but does the work and it's the simplest to write down.
63 All this will get integrated into the linker anyway, so there's no point
64 in doing optimizations
66 void collect_libs(const char *file, setnode **liblist_ptr)
68 unsigned long offset;
69 char type;
70 char secname[201];
71 char buff[256];
73 FILE *pipe = my_popen("nm ", file);
75 /* This fscanf() simply splits the whole stream into separate words */
76 while (fgets(buff, sizeof(buff), pipe)) {
77 struct setnode *node;
78 int pri;
80 offset = 0;
82 if (sscanf(buff, "%lx %c %200s ", &offset, &type, secname) != 3 &&
83 sscanf(buff, " %c %200s", &type, secname) != 2)
84 continue;
86 if (strncmp(secname, "__aros_libreq_", 14) != 0)
87 continue;
89 if (type == 'A') {
90 char *cp, *tmp;
92 cp = strchr(secname + 14, '.');
93 if (cp == NULL)
94 continue;
96 pri = strtoul(cp+1, &tmp, 0);
97 if ((cp+1) == tmp)
98 continue;
100 *(cp++) = 0;
101 } else if (type == 'w') {
102 pri = 0;
103 } else {
104 continue;
107 node = calloc(sizeof(*node),1);
108 node->secname = strdup(secname);
109 node->off_setname = 14;
110 node->pri = pri;
111 node->next = *liblist_ptr;
112 *liblist_ptr = node;
115 pclose(pipe);
119 int check_and_print_undefined_symbols(const char *file)
121 int there_are_undefined_syms = 0;
122 char buf[200];
123 size_t cnt;
125 FILE *pipe = my_popen(NM_NAME " -ulC ", file);
127 while ((cnt = fread(buf, 1, sizeof(buf), pipe)) != 0)
129 if (!there_are_undefined_syms)
131 there_are_undefined_syms = 1;
132 fprintf(stderr, "There are undefined symbols in '%s':\n", file);
135 fwrite(buf, cnt, 1, stderr);
138 pclose(pipe);
140 return there_are_undefined_syms;