1 | /* |
---|
2 | * Copyright (c) 2011, Chris Johns <chrisj@rtems.org> |
---|
3 | * |
---|
4 | * Permission to use, copy, modify, and/or distribute this software for any |
---|
5 | * purpose with or without fee is hereby granted, provided that the above |
---|
6 | * copyright notice and this permission notice appear in all copies. |
---|
7 | * |
---|
8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
---|
9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
---|
10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
---|
11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
---|
12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
---|
13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
---|
14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
---|
15 | */ |
---|
16 | /** |
---|
17 | * @file |
---|
18 | * |
---|
19 | * @ingroup rtems_ld |
---|
20 | * |
---|
21 | * @brief RTEMS Linker. |
---|
22 | * |
---|
23 | */ |
---|
24 | |
---|
25 | #if HAVE_CONFIG_H |
---|
26 | #include "config.h" |
---|
27 | #endif |
---|
28 | |
---|
29 | #include <iostream> |
---|
30 | |
---|
31 | #include <sys/stat.h> |
---|
32 | |
---|
33 | #include <rld.h> |
---|
34 | |
---|
35 | namespace rld |
---|
36 | { |
---|
37 | static int verbose_level = 0; |
---|
38 | |
---|
39 | /** |
---|
40 | * The program's command line. |
---|
41 | */ |
---|
42 | static std::string cmdline; |
---|
43 | |
---|
44 | /** |
---|
45 | * The program name as set by the caller. |
---|
46 | */ |
---|
47 | static std::string progname; |
---|
48 | |
---|
49 | /** |
---|
50 | * The option container. |
---|
51 | */ |
---|
52 | typedef std::vector < std::string > library_container; |
---|
53 | |
---|
54 | /** |
---|
55 | * The libraries the user provided on the command line. |
---|
56 | */ |
---|
57 | static library_container libpaths; |
---|
58 | |
---|
59 | /** |
---|
60 | * The libraries pass on the command line. |
---|
61 | */ |
---|
62 | static library_container libs; |
---|
63 | |
---|
64 | /** |
---|
65 | * The libraries. |
---|
66 | */ |
---|
67 | static library_container libraries; |
---|
68 | |
---|
69 | /** |
---|
70 | * The output passed on the command line. |
---|
71 | */ |
---|
72 | //static std::string output; |
---|
73 | |
---|
74 | bool |
---|
75 | starts_with(const std::string& s1, const std::string& s2) |
---|
76 | { |
---|
77 | return s2.size () <= s1.size () && s1.compare (0, s2.size (), s2) == 0; |
---|
78 | } |
---|
79 | |
---|
80 | const std::string |
---|
81 | ltrim (const std::string& s) |
---|
82 | { |
---|
83 | std::string t = s; |
---|
84 | t.erase (t.begin (), |
---|
85 | std::find_if (t.begin (), t.end (), |
---|
86 | std::not1 (std::ptr_fun < int, int > (std::isspace)))); |
---|
87 | return t; |
---|
88 | } |
---|
89 | |
---|
90 | const std::string |
---|
91 | rtrim (const std::string& s) |
---|
92 | { |
---|
93 | std::string t = s; |
---|
94 | t.erase (std::find_if (t.rbegin (), t.rend (), |
---|
95 | std::not1 (std::ptr_fun < int, int > (std::isspace))).base(), |
---|
96 | t.end()); |
---|
97 | return t; |
---|
98 | } |
---|
99 | |
---|
100 | const std::string |
---|
101 | trim (const std::string& s) |
---|
102 | { |
---|
103 | return ltrim (rtrim (s)); |
---|
104 | } |
---|
105 | |
---|
106 | const std::string |
---|
107 | dequote (const std::string& s) |
---|
108 | { |
---|
109 | if (!s.empty ()) |
---|
110 | { |
---|
111 | char front = s[0]; |
---|
112 | char back = s[s.length () - 1]; |
---|
113 | if ((front == '"') || (front == '\'')) |
---|
114 | { |
---|
115 | if (front != back) |
---|
116 | throw rld::error ("invalid quoting", "string: " + s); |
---|
117 | return s.substr (1, s.length () - (1 + 1)); |
---|
118 | } |
---|
119 | } |
---|
120 | return s; |
---|
121 | } |
---|
122 | |
---|
123 | const std::string |
---|
124 | find_replace(const std::string& sin, |
---|
125 | const std::string& out, |
---|
126 | const std::string& in) |
---|
127 | { |
---|
128 | std::string s = sin; |
---|
129 | size_t pos = 0; |
---|
130 | while ((pos = s.find (out, pos)) != std::string::npos) |
---|
131 | { |
---|
132 | s.replace (pos, out.length (), in); |
---|
133 | pos += in.length (); |
---|
134 | } |
---|
135 | return s; |
---|
136 | } |
---|
137 | |
---|
138 | const strings |
---|
139 | split (strings& se, |
---|
140 | const std::string& s, |
---|
141 | char delimiter, |
---|
142 | bool strip_quotes, |
---|
143 | bool strip_whitespace, |
---|
144 | bool empty) |
---|
145 | { |
---|
146 | std::stringstream ss(s); |
---|
147 | std::string e; |
---|
148 | se.clear (); |
---|
149 | while (std::getline (ss, e, delimiter)) |
---|
150 | { |
---|
151 | if (strip_whitespace) |
---|
152 | e = trim (e); |
---|
153 | if (strip_quotes) |
---|
154 | e = dequote (e); |
---|
155 | if (empty || !e.empty ()) |
---|
156 | { |
---|
157 | se.push_back (e); |
---|
158 | } |
---|
159 | } |
---|
160 | return se; |
---|
161 | } |
---|
162 | |
---|
163 | const std::string |
---|
164 | join (const strings& ss, const std::string& separator) |
---|
165 | { |
---|
166 | std::string s; |
---|
167 | for (strings::const_iterator ssi = ss.begin (); |
---|
168 | ssi != ss.end (); |
---|
169 | ++ssi) |
---|
170 | { |
---|
171 | s += *ssi; |
---|
172 | if ((ssi + 1) != ss.end ()) |
---|
173 | s += separator; |
---|
174 | } |
---|
175 | return s; |
---|
176 | } |
---|
177 | |
---|
178 | const std::string |
---|
179 | tolower (const std::string& sin) |
---|
180 | { |
---|
181 | std::string s = sin; |
---|
182 | std::transform (s.begin (), s.end (), s.begin (), ::tolower); |
---|
183 | return s; |
---|
184 | } |
---|
185 | |
---|
186 | void |
---|
187 | verbose_inc () |
---|
188 | { |
---|
189 | ++verbose_level; |
---|
190 | } |
---|
191 | |
---|
192 | int |
---|
193 | verbose (int level) |
---|
194 | { |
---|
195 | return verbose_level && (verbose_level >= level) ? verbose_level : 0; |
---|
196 | } |
---|
197 | |
---|
198 | const std::string |
---|
199 | version () |
---|
200 | { |
---|
201 | return RTEMS_RELEASE; |
---|
202 | } |
---|
203 | |
---|
204 | const std::string |
---|
205 | rtems_version () |
---|
206 | { |
---|
207 | return RTEMS_VERSION; |
---|
208 | } |
---|
209 | |
---|
210 | void |
---|
211 | set_cmdline (int argc, char* argv[]) |
---|
212 | { |
---|
213 | cmdline.clear (); |
---|
214 | for (int arg = 0; arg < argc; ++arg) |
---|
215 | { |
---|
216 | std::string a = argv[arg]; |
---|
217 | cmdline += ' ' + a; |
---|
218 | } |
---|
219 | cmdline = rld::trim (cmdline); |
---|
220 | } |
---|
221 | |
---|
222 | const std::string |
---|
223 | get_cmdline () |
---|
224 | { |
---|
225 | return cmdline; |
---|
226 | } |
---|
227 | |
---|
228 | void |
---|
229 | set_progname (const std::string& progname_) |
---|
230 | { |
---|
231 | if (rld::path::check_file (progname_)) |
---|
232 | progname = rld::path::path_abs (progname_); |
---|
233 | else |
---|
234 | { |
---|
235 | rld::path::paths paths; |
---|
236 | rld::path::get_system_path (paths); |
---|
237 | for (rld::path::paths::const_iterator path = paths.begin (); |
---|
238 | path != paths.end (); |
---|
239 | ++path) |
---|
240 | { |
---|
241 | std::string pp; |
---|
242 | rld::path::path_join (*path, progname_, pp); |
---|
243 | if (rld::path::check_file (pp)) |
---|
244 | { |
---|
245 | progname = rld::path::path_abs (pp); |
---|
246 | break; |
---|
247 | } |
---|
248 | } |
---|
249 | } |
---|
250 | } |
---|
251 | |
---|
252 | const std::string |
---|
253 | get_progname () |
---|
254 | { |
---|
255 | return progname; |
---|
256 | } |
---|
257 | |
---|
258 | const std::string |
---|
259 | get_program_name () |
---|
260 | { |
---|
261 | return rld::path::basename (progname); |
---|
262 | } |
---|
263 | |
---|
264 | const std::string |
---|
265 | get_program_path () |
---|
266 | { |
---|
267 | return rld::path::dirname (progname); |
---|
268 | } |
---|
269 | |
---|
270 | const std::string |
---|
271 | get_prefix () |
---|
272 | { |
---|
273 | std::string pp = get_program_path (); |
---|
274 | if (rld::path::basename (pp) == "bin") |
---|
275 | return rld::path::dirname (pp); |
---|
276 | return pp; |
---|
277 | } |
---|
278 | |
---|
279 | void |
---|
280 | map (rld::files::cache& cache, rld::symbols::table& symbols) |
---|
281 | { |
---|
282 | std::cout << "Archive files : " << cache.archive_count () << std::endl; |
---|
283 | std::cout << "Object files : " << cache.object_count () << std::endl; |
---|
284 | std::cout << "Exported symbols : " << symbols.size () << std::endl; |
---|
285 | |
---|
286 | std::cout << "Archives:" << std::endl; |
---|
287 | cache.output_archive_files (std::cout); |
---|
288 | std::cout << "Objects:" << std::endl; |
---|
289 | cache.output_object_files (std::cout); |
---|
290 | |
---|
291 | std::cout << "Exported symbols:" << std::endl; |
---|
292 | rld::symbols::output (std::cout, symbols); |
---|
293 | std::cout << "Unresolved symbols:" << std::endl; |
---|
294 | cache.output_unresolved_symbols (std::cout); |
---|
295 | } |
---|
296 | |
---|
297 | void |
---|
298 | warn_unused_externals (rld::files::object_list& objects) |
---|
299 | { |
---|
300 | bool first = true; |
---|
301 | for (rld::files::object_list::iterator oli = objects.begin (); |
---|
302 | oli != objects.end (); |
---|
303 | ++oli) |
---|
304 | { |
---|
305 | rld::files::object& object = *(*oli); |
---|
306 | rld::symbols::pointers& externals = object.external_symbols (); |
---|
307 | |
---|
308 | if (rld::symbols::referenced (externals) != externals.size ()) |
---|
309 | { |
---|
310 | if (first) |
---|
311 | { |
---|
312 | std::cout << "Unreferenced externals in object files:" << std::endl; |
---|
313 | first = false; |
---|
314 | } |
---|
315 | |
---|
316 | std::cout << ' ' << object.name ().basename () << std::endl; |
---|
317 | |
---|
318 | for (rld::symbols::pointers::iterator sli = externals.begin (); |
---|
319 | sli != externals.end (); |
---|
320 | ++sli) |
---|
321 | { |
---|
322 | rld::symbols::symbol& sym = *(*sli); |
---|
323 | if (sym.references () == 0) |
---|
324 | std::cout << " " << sym.name () << std::endl; |
---|
325 | } |
---|
326 | } |
---|
327 | } |
---|
328 | } |
---|
329 | |
---|
330 | } |
---|