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 <cxxabi.h> |
---|
32 | #include <sys/stat.h> |
---|
33 | |
---|
34 | #include <rld.h> |
---|
35 | |
---|
36 | namespace rld |
---|
37 | { |
---|
38 | static int verbose_level = 0; |
---|
39 | |
---|
40 | /** |
---|
41 | * The program's command line. |
---|
42 | */ |
---|
43 | static std::string cmdline; |
---|
44 | |
---|
45 | /** |
---|
46 | * The program name as set by the caller. |
---|
47 | */ |
---|
48 | static std::string progname; |
---|
49 | |
---|
50 | /** |
---|
51 | * The option container. |
---|
52 | */ |
---|
53 | typedef std::vector < std::string > library_container; |
---|
54 | |
---|
55 | /** |
---|
56 | * The libraries the user provided on the command line. |
---|
57 | */ |
---|
58 | static library_container libpaths; |
---|
59 | |
---|
60 | /** |
---|
61 | * The libraries pass on the command line. |
---|
62 | */ |
---|
63 | static library_container libs; |
---|
64 | |
---|
65 | /** |
---|
66 | * The libraries. |
---|
67 | */ |
---|
68 | static library_container libraries; |
---|
69 | |
---|
70 | /** |
---|
71 | * The version major number. |
---|
72 | */ |
---|
73 | static uint64_t _version_major; |
---|
74 | |
---|
75 | /** |
---|
76 | * The version minor number. |
---|
77 | */ |
---|
78 | static uint64_t _version_minor; |
---|
79 | |
---|
80 | /** |
---|
81 | * The version revision number. |
---|
82 | */ |
---|
83 | static uint64_t _version_revision; |
---|
84 | |
---|
85 | void |
---|
86 | output_std_exception (std::exception e, std::ostream& out) |
---|
87 | { |
---|
88 | int status; |
---|
89 | char* realname; |
---|
90 | realname = abi::__cxa_demangle (e.what(), 0, 0, &status); |
---|
91 | out << "error: exception: " << realname << " ["; |
---|
92 | ::free (realname); |
---|
93 | const std::type_info &ti = typeid (e); |
---|
94 | realname = abi::__cxa_demangle (ti.name(), 0, 0, &status); |
---|
95 | out << realname << "] " << e.what () << std::endl; |
---|
96 | ::free (realname); |
---|
97 | } |
---|
98 | |
---|
99 | bool |
---|
100 | starts_with(const std::string& s1, const std::string& s2) |
---|
101 | { |
---|
102 | return s2.size () <= s1.size () && s1.compare (0, s2.size (), s2) == 0; |
---|
103 | } |
---|
104 | |
---|
105 | const std::string |
---|
106 | ltrim (const std::string& s) |
---|
107 | { |
---|
108 | std::string t = s; |
---|
109 | auto non_space = |
---|
110 | std::find_if (t.begin (), t.end (), |
---|
111 | [](unsigned char c) { return !std::isspace (c); }); |
---|
112 | t.erase (t.begin (), non_space); |
---|
113 | return t; |
---|
114 | } |
---|
115 | |
---|
116 | const std::string |
---|
117 | rtrim (const std::string& s) |
---|
118 | { |
---|
119 | std::string t = s; |
---|
120 | auto last_space = |
---|
121 | std::find_if (t.rbegin (), t.rend (), |
---|
122 | [](unsigned char c) { return !std::isspace (c); }).base(); |
---|
123 | t.erase (last_space, t.end()); |
---|
124 | return t; |
---|
125 | } |
---|
126 | |
---|
127 | const std::string |
---|
128 | trim (const std::string& s) |
---|
129 | { |
---|
130 | return ltrim (rtrim (s)); |
---|
131 | } |
---|
132 | |
---|
133 | const std::string |
---|
134 | dequote (const std::string& s) |
---|
135 | { |
---|
136 | if (!s.empty ()) |
---|
137 | { |
---|
138 | char front = s[0]; |
---|
139 | char back = s[s.length () - 1]; |
---|
140 | if ((front == '"') || (front == '\'')) |
---|
141 | { |
---|
142 | if (front != back) |
---|
143 | throw rld::error ("invalid quoting", "string: " + s); |
---|
144 | return s.substr (1, s.length () - (1 + 1)); |
---|
145 | } |
---|
146 | } |
---|
147 | return s; |
---|
148 | } |
---|
149 | |
---|
150 | const std::string |
---|
151 | find_replace(const std::string& sin, |
---|
152 | const std::string& out, |
---|
153 | const std::string& in) |
---|
154 | { |
---|
155 | std::string s = sin; |
---|
156 | size_t pos = 0; |
---|
157 | while ((pos = s.find (out, pos)) != std::string::npos) |
---|
158 | { |
---|
159 | s.replace (pos, out.length (), in); |
---|
160 | pos += in.length (); |
---|
161 | } |
---|
162 | return s; |
---|
163 | } |
---|
164 | |
---|
165 | const strings |
---|
166 | split (strings& se, |
---|
167 | const std::string& s, |
---|
168 | char delimiter, |
---|
169 | bool strip_quotes, |
---|
170 | bool strip_whitespace, |
---|
171 | bool empty) |
---|
172 | { |
---|
173 | std::stringstream ss(s); |
---|
174 | std::string e; |
---|
175 | se.clear (); |
---|
176 | while (std::getline (ss, e, delimiter)) |
---|
177 | { |
---|
178 | if (strip_whitespace) |
---|
179 | e = trim (e); |
---|
180 | if (strip_quotes) |
---|
181 | e = dequote (e); |
---|
182 | if (empty || !e.empty ()) |
---|
183 | { |
---|
184 | se.push_back (e); |
---|
185 | } |
---|
186 | } |
---|
187 | return se; |
---|
188 | } |
---|
189 | |
---|
190 | const std::string |
---|
191 | join (const strings& ss, const std::string& separator) |
---|
192 | { |
---|
193 | std::string s; |
---|
194 | for (strings::const_iterator ssi = ss.begin (); |
---|
195 | ssi != ss.end (); |
---|
196 | ++ssi) |
---|
197 | { |
---|
198 | s += *ssi; |
---|
199 | if ((ssi + 1) != ss.end ()) |
---|
200 | s += separator; |
---|
201 | } |
---|
202 | return s; |
---|
203 | } |
---|
204 | |
---|
205 | const std::string |
---|
206 | tolower (const std::string& sin) |
---|
207 | { |
---|
208 | std::string s = sin; |
---|
209 | std::transform (s.begin (), s.end (), s.begin (), ::tolower); |
---|
210 | return s; |
---|
211 | } |
---|
212 | |
---|
213 | void |
---|
214 | version_parse (const std::string& str, |
---|
215 | uint64_t& major, |
---|
216 | uint64_t& minor, |
---|
217 | uint64_t& revision) |
---|
218 | { |
---|
219 | strings parts; |
---|
220 | |
---|
221 | rld::split (parts, str, '.'); |
---|
222 | |
---|
223 | if (parts.size () >= 1) |
---|
224 | { |
---|
225 | std::istringstream iss (parts[0]); |
---|
226 | iss >> major; |
---|
227 | } |
---|
228 | |
---|
229 | if (parts.size () >= 2) |
---|
230 | { |
---|
231 | std::istringstream iss (parts[1]); |
---|
232 | iss >> minor; |
---|
233 | } |
---|
234 | |
---|
235 | if (parts.size () >= 3) |
---|
236 | { |
---|
237 | size_t p = parts[2].find ('_'); |
---|
238 | |
---|
239 | if (p != std::string::npos) |
---|
240 | parts[2].erase (p); |
---|
241 | |
---|
242 | std::istringstream iss (parts[2]); |
---|
243 | |
---|
244 | if (p != std::string::npos) |
---|
245 | iss >> std::hex; |
---|
246 | |
---|
247 | iss >> revision; |
---|
248 | } |
---|
249 | } |
---|
250 | |
---|
251 | void |
---|
252 | verbose_inc () |
---|
253 | { |
---|
254 | ++verbose_level; |
---|
255 | } |
---|
256 | |
---|
257 | int |
---|
258 | verbose (int level) |
---|
259 | { |
---|
260 | return verbose_level && (verbose_level >= level) ? verbose_level : 0; |
---|
261 | } |
---|
262 | |
---|
263 | const std::string |
---|
264 | version () |
---|
265 | { |
---|
266 | return RTEMS_RELEASE; |
---|
267 | } |
---|
268 | |
---|
269 | uint64_t |
---|
270 | version_major () |
---|
271 | { |
---|
272 | if (_version_major == 0) |
---|
273 | version_parse (version (), |
---|
274 | _version_major, |
---|
275 | _version_minor, |
---|
276 | _version_revision); |
---|
277 | return _version_major; |
---|
278 | } |
---|
279 | |
---|
280 | uint64_t |
---|
281 | version_minor () |
---|
282 | { |
---|
283 | if (_version_major == 0) |
---|
284 | version_parse (version (), |
---|
285 | _version_major, |
---|
286 | _version_minor, |
---|
287 | _version_revision); |
---|
288 | return _version_minor; |
---|
289 | } |
---|
290 | |
---|
291 | uint64_t |
---|
292 | version_revision () |
---|
293 | { |
---|
294 | if (_version_major == 0) |
---|
295 | version_parse (version (), |
---|
296 | _version_major, |
---|
297 | _version_minor, |
---|
298 | _version_revision); |
---|
299 | return _version_revision; |
---|
300 | } |
---|
301 | |
---|
302 | void |
---|
303 | set_cmdline (int argc, char* argv[]) |
---|
304 | { |
---|
305 | cmdline.clear (); |
---|
306 | for (int arg = 0; arg < argc; ++arg) |
---|
307 | { |
---|
308 | std::string a = argv[arg]; |
---|
309 | cmdline += ' ' + a; |
---|
310 | } |
---|
311 | cmdline = rld::trim (cmdline); |
---|
312 | } |
---|
313 | |
---|
314 | const std::string |
---|
315 | get_cmdline () |
---|
316 | { |
---|
317 | return cmdline; |
---|
318 | } |
---|
319 | |
---|
320 | void |
---|
321 | set_progname (const std::string& progname_) |
---|
322 | { |
---|
323 | if (rld::path::check_file (progname_)) |
---|
324 | progname = rld::path::path_abs (progname_); |
---|
325 | else |
---|
326 | { |
---|
327 | rld::path::paths paths; |
---|
328 | rld::path::get_system_path (paths); |
---|
329 | for (rld::path::paths::const_iterator path = paths.begin (); |
---|
330 | path != paths.end (); |
---|
331 | ++path) |
---|
332 | { |
---|
333 | std::string pp; |
---|
334 | rld::path::path_join (*path, progname_, pp); |
---|
335 | if (rld::path::check_file (pp)) |
---|
336 | { |
---|
337 | progname = rld::path::path_abs (pp); |
---|
338 | break; |
---|
339 | } |
---|
340 | } |
---|
341 | } |
---|
342 | } |
---|
343 | |
---|
344 | const std::string |
---|
345 | get_progname () |
---|
346 | { |
---|
347 | return progname; |
---|
348 | } |
---|
349 | |
---|
350 | const std::string |
---|
351 | get_program_name () |
---|
352 | { |
---|
353 | return rld::path::basename (progname); |
---|
354 | } |
---|
355 | |
---|
356 | const std::string |
---|
357 | get_program_path () |
---|
358 | { |
---|
359 | return rld::path::dirname (progname); |
---|
360 | } |
---|
361 | |
---|
362 | const std::string |
---|
363 | get_prefix () |
---|
364 | { |
---|
365 | std::string pp = get_program_path (); |
---|
366 | if (rld::path::basename (pp) == "bin") |
---|
367 | return rld::path::dirname (pp); |
---|
368 | return pp; |
---|
369 | } |
---|
370 | |
---|
371 | void |
---|
372 | map (rld::files::cache& cache, rld::symbols::table& symbols) |
---|
373 | { |
---|
374 | std::cout << "Archive files : " << cache.archive_count () << std::endl; |
---|
375 | std::cout << "Object files : " << cache.object_count () << std::endl; |
---|
376 | std::cout << "Exported symbols : " << symbols.size () << std::endl; |
---|
377 | |
---|
378 | std::cout << "Archives:" << std::endl; |
---|
379 | cache.output_archive_files (std::cout); |
---|
380 | std::cout << "Objects:" << std::endl; |
---|
381 | cache.output_object_files (std::cout); |
---|
382 | |
---|
383 | std::cout << "Exported symbols:" << std::endl; |
---|
384 | rld::symbols::output (std::cout, symbols); |
---|
385 | std::cout << "Unresolved symbols:" << std::endl; |
---|
386 | cache.output_unresolved_symbols (std::cout); |
---|
387 | } |
---|
388 | |
---|
389 | void |
---|
390 | warn_unused_externals (rld::files::object_list& objects) |
---|
391 | { |
---|
392 | bool first = true; |
---|
393 | for (rld::files::object_list::iterator oli = objects.begin (); |
---|
394 | oli != objects.end (); |
---|
395 | ++oli) |
---|
396 | { |
---|
397 | rld::files::object& object = *(*oli); |
---|
398 | rld::symbols::pointers& externals = object.external_symbols (); |
---|
399 | |
---|
400 | if (rld::symbols::referenced (externals) != externals.size ()) |
---|
401 | { |
---|
402 | if (first) |
---|
403 | { |
---|
404 | std::cout << "Unreferenced externals in object files:" << std::endl; |
---|
405 | first = false; |
---|
406 | } |
---|
407 | |
---|
408 | std::cout << ' ' << object.name ().basename () << std::endl; |
---|
409 | |
---|
410 | for (rld::symbols::pointers::iterator sli = externals.begin (); |
---|
411 | sli != externals.end (); |
---|
412 | ++sli) |
---|
413 | { |
---|
414 | rld::symbols::symbol& sym = *(*sli); |
---|
415 | if (sym.references () == 0) |
---|
416 | std::cout << " " << sym.name () << std::endl; |
---|
417 | } |
---|
418 | } |
---|
419 | } |
---|
420 | } |
---|
421 | |
---|
422 | } |
---|