source: rtems-tools/linkers/rld.h @ e5165d2

4.104.115
Last change on this file since e5165d2 was 097f1fd, checked in by Chris Johns <chrisj@…>, on 08/07/14 at 08:15:06

rtms-tld: Refactor the code to match a better configuration format.

  • Property mode set to 100644
File size: 6.4 KB
Line 
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
53namespace 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
83namespace 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.empty ())
160    {
161      char front = s[0];
162      char back = s[s.length () - 1];
163      if ((front == '"') || (front == '\''))
164      {
165        if (front != back)
166          throw rld::error ("invalid quoting", "string: " + s);
167        return s.substr (1, s.length () - (1 + 1));
168      }
169    }
170    return s;
171  }
172
173  /**
174   * Find and replace.
175   */
176  inline std::string find_replace(const std::string& sin,
177                                  const std::string& out,
178                                  const std::string& in)
179  {
180    std::string s = sin;
181    size_t      pos = 0;
182    while ((pos = s.find (out, pos)) != std::string::npos)
183    {
184      s.replace (pos, out.length (), in);
185      pos += in.length ();
186    }
187    return s;
188  }
189
190  /**
191   * Split the string in a contain of strings based on the the
192   * delimiter. Optionally trim any white space or include empty string.
193   *
194   * @todo The split should optionally honour string quoting.
195   */
196  inline strings& split (strings&           se,
197                         const std::string& s,
198                         char               delimiter,
199                         bool               strip_quotes = true,
200                         bool               strip_whitespace = true,
201                         bool               empty = false)
202  {
203    std::stringstream ss(s);
204    std::string       e;
205    se.clear ();
206    while (std::getline (ss, e, delimiter))
207    {
208      if (strip_whitespace)
209        trim (e);
210      if (strip_quotes)
211        e = dequote (e);
212      if (empty || !e.empty ())
213      {
214        se.push_back (e);
215      }
216    }
217    return se;
218  }
219
220  /**
221   * Join the strings together with the separator.
222   */
223  inline std::string join (const strings&     ss,
224                           const std::string& separator)
225  {
226    std::string s;
227    for (strings::const_iterator ssi = ss.begin ();
228         ssi != ss.end ();
229         ++ssi)
230    {
231      s += *ssi;
232      if ((ssi != ss.begin ()) && (ssi != ss.end ()))
233        s += separator;
234    }
235    return s;
236  }
237
238  /**
239   * Increment the verbose level.
240   */
241  void verbose_inc ();
242
243  /**
244   * Return the verbose level. Setting the flag more than once raises the
245   * level.
246   */
247  int verbose ();
248
249  /**
250   * The version string.
251   */
252  const std::string version ();
253
254  /**
255   * The RTEMS version string.
256   */
257  const std::string rtems_version ();
258
259  /**
260   * Container of strings to hold the results of a split.
261   */
262  typedef std::vector < std::string > strings;
263
264  /**
265   * Split a string into strings by the separator.
266   */
267  void split (const std::string& str, strings& strs, char separator);
268
269  /**
270   * Map of the symbol table.
271   */
272  void map (rld::files::cache& cache, rld::symbols::table& symbols);
273
274  /**
275   * Warn is externals in referenced object files are not used.
276   */
277  void warn_unused_externals (rld::files::object_list& objects);
278}
279
280#endif
Note: See TracBrowser for help on using the repository browser.