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 readies the RTEMS object files for dynamic linking. |
---|
22 | * |
---|
23 | */ |
---|
24 | |
---|
25 | #if !defined (_RLD_H_) |
---|
26 | #define _RLD_H_ |
---|
27 | |
---|
28 | #include <algorithm> |
---|
29 | #include <cctype> |
---|
30 | #include <functional> |
---|
31 | #include <iostream> |
---|
32 | #include <locale> |
---|
33 | #include <sstream> |
---|
34 | #include <string> |
---|
35 | |
---|
36 | /** |
---|
37 | * Path handling for Windows. |
---|
38 | */ |
---|
39 | #if __WIN32__ |
---|
40 | #define RLD_PATH_SEPARATOR '\\' |
---|
41 | #define RLD_PATHSTR_SEPARATOR ';' |
---|
42 | #define RLD_PATHSTR_SEPARATOR_STR ";" |
---|
43 | #define RLD_DRIVE_SEPARATOR (1) |
---|
44 | #define RLD_LINE_SEPARATOR "\r\n" |
---|
45 | #else |
---|
46 | #define RLD_PATH_SEPARATOR '/' |
---|
47 | #define RLD_PATHSTR_SEPARATOR ':' |
---|
48 | #define RLD_PATHSTR_SEPARATOR_STR ":" |
---|
49 | #define RLD_DRIVE_SEPARATOR (0) |
---|
50 | #define RLD_LINE_SEPARATOR "\n" |
---|
51 | #endif |
---|
52 | |
---|
53 | namespace rld |
---|
54 | { |
---|
55 | /** |
---|
56 | * Forward declarations. |
---|
57 | */ |
---|
58 | namespace files |
---|
59 | { |
---|
60 | class file; |
---|
61 | class image; |
---|
62 | class archive; |
---|
63 | class object; |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | #include <rld-elf-types.h> |
---|
68 | #include <rld-symbols.h> |
---|
69 | #include <rld-elf.h> |
---|
70 | #include <rld-files.h> |
---|
71 | |
---|
72 | /** |
---|
73 | * The debug levels. |
---|
74 | */ |
---|
75 | #define RLD_VERBOSE_OFF (0) |
---|
76 | #define RLD_VERBOSE_INFO (1) |
---|
77 | #define RLD_VERBOSE_DETAILS (2) |
---|
78 | #define RLD_VERBOSE_TRACE (3) |
---|
79 | #define RLD_VERBOSE_TRACE_SYMS (4) |
---|
80 | #define RLD_VERBOSE_TRACE_FILE (5) |
---|
81 | #define RLD_VERBOSE_FULL_DEBUG (6) |
---|
82 | |
---|
83 | namespace rld |
---|
84 | { |
---|
85 | /** |
---|
86 | * General error. |
---|
87 | */ |
---|
88 | struct error |
---|
89 | { |
---|
90 | const std::string what; |
---|
91 | const std::string where; |
---|
92 | |
---|
93 | error (const std::ostringstream& what, const std::string& where) : |
---|
94 | what (what.str ()), where (where) { |
---|
95 | } |
---|
96 | |
---|
97 | error (const std::string& what, const std::string& where) : |
---|
98 | what (what), where (where) { |
---|
99 | } |
---|
100 | }; |
---|
101 | |
---|
102 | /** |
---|
103 | * A convenience macro to make where a file and line number. |
---|
104 | */ |
---|
105 | #define rld_error_at(_what) \ |
---|
106 | rld::error (_what, std::string (__FILE__) + ":" + to_string (__LINE__)) |
---|
107 | |
---|
108 | /** |
---|
109 | * Convert a supported type to a string. |
---|
110 | */ |
---|
111 | template <class T> |
---|
112 | std::string to_string (T t, std::ios_base & (*f)(std::ios_base&) = std::dec) |
---|
113 | { |
---|
114 | std::ostringstream oss; |
---|
115 | oss << f << t; |
---|
116 | return oss.str(); |
---|
117 | } |
---|
118 | |
---|
119 | /** |
---|
120 | * A container of strings. |
---|
121 | */ |
---|
122 | typedef std::vector < std::string > strings; |
---|
123 | |
---|
124 | /** |
---|
125 | * Trim from start. |
---|
126 | */ |
---|
127 | inline std::string& ltrim (std::string &s) |
---|
128 | { |
---|
129 | s.erase (s.begin (), |
---|
130 | std::find_if (s.begin (), s.end (), |
---|
131 | std::not1 (std::ptr_fun < int, int > (std::isspace)))); |
---|
132 | return s; |
---|
133 | } |
---|
134 | |
---|
135 | /** |
---|
136 | * Trim from end. |
---|
137 | */ |
---|
138 | inline std::string& rtrim (std::string &s) |
---|
139 | { |
---|
140 | s.erase (std::find_if (s.rbegin (), s.rend (), |
---|
141 | std::not1 (std::ptr_fun < int, int > (std::isspace))).base(), |
---|
142 | s.end()); |
---|
143 | return s; |
---|
144 | } |
---|
145 | |
---|
146 | /** |
---|
147 | * Trim from both ends. |
---|
148 | */ |
---|
149 | inline std::string& trim (std::string &s) |
---|
150 | { |
---|
151 | return ltrim (rtrim (s)); |
---|
152 | } |
---|
153 | |
---|
154 | /** |
---|
155 | * Dequote a string. |
---|
156 | */ |
---|
157 | inline std::string dequote (const std::string& s) |
---|
158 | { |
---|
159 | if ((s.front () == '"') || (s.front () == '\'')) |
---|
160 | { |
---|
161 | if (s.front () != s.back ()) |
---|
162 | throw rld::error ("invalid quoting", "string: " + s); |
---|
163 | return s.substr (1, s.length () - (1 + 1)); |
---|
164 | } |
---|
165 | return s; |
---|
166 | } |
---|
167 | |
---|
168 | /** |
---|
169 | * Find and replace. |
---|
170 | */ |
---|
171 | inline std::string find_replace(const std::string& sin, |
---|
172 | const std::string& out, |
---|
173 | const std::string& in) |
---|
174 | { |
---|
175 | std::string s = sin; |
---|
176 | size_t pos = 0; |
---|
177 | while ((pos = s.find (out, pos)) != std::string::npos) |
---|
178 | { |
---|
179 | s.replace (pos, out.length (), in); |
---|
180 | pos += in.length (); |
---|
181 | } |
---|
182 | return s; |
---|
183 | } |
---|
184 | |
---|
185 | /** |
---|
186 | * Split the string in a contain of strings based on the the |
---|
187 | * delimiter. Optionally trim any white space or include empty string. |
---|
188 | * |
---|
189 | * @todo The split should optionally honour string quoting. |
---|
190 | */ |
---|
191 | inline strings& split (strings& se, |
---|
192 | const std::string& s, |
---|
193 | char delimiter, |
---|
194 | bool strip_quotes = true, |
---|
195 | bool strip_whitespace = true, |
---|
196 | bool empty = false) |
---|
197 | { |
---|
198 | std::stringstream ss(s); |
---|
199 | std::string e; |
---|
200 | se.clear (); |
---|
201 | while (std::getline (ss, e, delimiter)) |
---|
202 | { |
---|
203 | if (strip_whitespace) |
---|
204 | trim (e); |
---|
205 | if (strip_quotes) |
---|
206 | e = dequote (e); |
---|
207 | if (empty || !e.empty ()) |
---|
208 | { |
---|
209 | se.push_back (e); |
---|
210 | } |
---|
211 | } |
---|
212 | return se; |
---|
213 | } |
---|
214 | |
---|
215 | /** |
---|
216 | * Join the strings together with the separator. |
---|
217 | */ |
---|
218 | inline std::string join (const strings& ss, |
---|
219 | const std::string& separator) |
---|
220 | { |
---|
221 | std::string s; |
---|
222 | for (strings::const_iterator ssi = ss.begin (); |
---|
223 | ssi != ss.end (); |
---|
224 | ++ssi) |
---|
225 | { |
---|
226 | s += *ssi; |
---|
227 | if ((ssi != ss.begin ()) && (ssi != ss.end ())) |
---|
228 | s += separator; |
---|
229 | } |
---|
230 | return s; |
---|
231 | } |
---|
232 | |
---|
233 | /** |
---|
234 | * Increment the verbose level. |
---|
235 | */ |
---|
236 | void verbose_inc (); |
---|
237 | |
---|
238 | /** |
---|
239 | * Return the verbose level. Setting the flag more than once raises the |
---|
240 | * level. |
---|
241 | */ |
---|
242 | int verbose (); |
---|
243 | |
---|
244 | /** |
---|
245 | * The version string. |
---|
246 | */ |
---|
247 | const std::string version (); |
---|
248 | |
---|
249 | /** |
---|
250 | * The RTEMS version string. |
---|
251 | */ |
---|
252 | const std::string rtems_version (); |
---|
253 | |
---|
254 | /** |
---|
255 | * Container of strings to hold the results of a split. |
---|
256 | */ |
---|
257 | typedef std::vector < std::string > strings; |
---|
258 | |
---|
259 | /** |
---|
260 | * Split a string into strings by the separator. |
---|
261 | */ |
---|
262 | void split (const std::string& str, strings& strs, char separator); |
---|
263 | |
---|
264 | /** |
---|
265 | * Map of the symbol table. |
---|
266 | */ |
---|
267 | void map (rld::files::cache& cache, rld::symbols::table& symbols); |
---|
268 | |
---|
269 | /** |
---|
270 | * Warn is externals in referenced object files are not used. |
---|
271 | */ |
---|
272 | void warn_unused_externals (rld::files::object_list& objects); |
---|
273 | } |
---|
274 | |
---|
275 | #endif |
---|