xref: /dragonfly/contrib/binutils-2.27/gold/gold.h (revision a9fa9459)
1*a9fa9459Szrj // gold.h -- general definitions for gold   -*- C++ -*-
2*a9fa9459Szrj 
3*a9fa9459Szrj // Copyright (C) 2006-2016 Free Software Foundation, Inc.
4*a9fa9459Szrj // Written by Ian Lance Taylor <iant@google.com>.
5*a9fa9459Szrj 
6*a9fa9459Szrj // This file is part of gold.
7*a9fa9459Szrj 
8*a9fa9459Szrj // This program is free software; you can redistribute it and/or modify
9*a9fa9459Szrj // it under the terms of the GNU General Public License as published by
10*a9fa9459Szrj // the Free Software Foundation; either version 3 of the License, or
11*a9fa9459Szrj // (at your option) any later version.
12*a9fa9459Szrj 
13*a9fa9459Szrj // This program is distributed in the hope that it will be useful,
14*a9fa9459Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of
15*a9fa9459Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16*a9fa9459Szrj // GNU General Public License for more details.
17*a9fa9459Szrj 
18*a9fa9459Szrj // You should have received a copy of the GNU General Public License
19*a9fa9459Szrj // along with this program; if not, write to the Free Software
20*a9fa9459Szrj // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21*a9fa9459Szrj // MA 02110-1301, USA.
22*a9fa9459Szrj 
23*a9fa9459Szrj #ifndef GOLD_GOLD_H
24*a9fa9459Szrj #define GOLD_GOLD_H
25*a9fa9459Szrj 
26*a9fa9459Szrj #include "config.h"
27*a9fa9459Szrj #include "ansidecl.h"
28*a9fa9459Szrj 
29*a9fa9459Szrj #include <cstddef>
30*a9fa9459Szrj #include <cstdlib>
31*a9fa9459Szrj #include <cstring>
32*a9fa9459Szrj #include <stdint.h>
33*a9fa9459Szrj #include <sys/types.h>
34*a9fa9459Szrj 
35*a9fa9459Szrj #include "system.h"
36*a9fa9459Szrj 
37*a9fa9459Szrj namespace gold
38*a9fa9459Szrj {
39*a9fa9459Szrj 
40*a9fa9459Szrj // General declarations.
41*a9fa9459Szrj 
42*a9fa9459Szrj class General_options;
43*a9fa9459Szrj class Command_line;
44*a9fa9459Szrj class Dirsearch;
45*a9fa9459Szrj class Input_objects;
46*a9fa9459Szrj class Mapfile;
47*a9fa9459Szrj class Symbol;
48*a9fa9459Szrj class Symbol_table;
49*a9fa9459Szrj class Layout;
50*a9fa9459Szrj class Task;
51*a9fa9459Szrj class Workqueue;
52*a9fa9459Szrj class Output_file;
53*a9fa9459Szrj template<int size, bool big_endian>
54*a9fa9459Szrj struct Relocate_info;
55*a9fa9459Szrj 
56*a9fa9459Szrj // Exit status codes.
57*a9fa9459Szrj 
58*a9fa9459Szrj enum Exit_status
59*a9fa9459Szrj {
60*a9fa9459Szrj   GOLD_OK = EXIT_SUCCESS,
61*a9fa9459Szrj   GOLD_ERR = EXIT_FAILURE,
62*a9fa9459Szrj   GOLD_FALLBACK = EXIT_FAILURE + 1
63*a9fa9459Szrj };
64*a9fa9459Szrj 
65*a9fa9459Szrj // Some basic types.  For these we use lower case initial letters.
66*a9fa9459Szrj 
67*a9fa9459Szrj // For an offset in an input or output file, use off_t.  Note that
68*a9fa9459Szrj // this will often be a 64-bit type even for a 32-bit build.
69*a9fa9459Szrj 
70*a9fa9459Szrj // The size of a section if we are going to look at the contents.
71*a9fa9459Szrj typedef size_t section_size_type;
72*a9fa9459Szrj 
73*a9fa9459Szrj // An offset within a section when we are looking at the contents.
74*a9fa9459Szrj typedef ptrdiff_t section_offset_type;
75*a9fa9459Szrj 
76*a9fa9459Szrj // The name of the program as used in error messages.
77*a9fa9459Szrj extern const char* program_name;
78*a9fa9459Szrj 
79*a9fa9459Szrj // This function is called to exit the program.  Status is true to
80*a9fa9459Szrj // exit success (0) and false to exit failure (1).
81*a9fa9459Szrj extern void
82*a9fa9459Szrj gold_exit(Exit_status status) ATTRIBUTE_NORETURN;
83*a9fa9459Szrj 
84*a9fa9459Szrj // This function is called to emit an error message and then
85*a9fa9459Szrj // immediately exit with failure.
86*a9fa9459Szrj extern void
87*a9fa9459Szrj gold_fatal(const char* format, ...) ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF_1;
88*a9fa9459Szrj 
89*a9fa9459Szrj // This function is called to issue an error.  This will cause gold to
90*a9fa9459Szrj // eventually exit with failure.
91*a9fa9459Szrj extern void
92*a9fa9459Szrj gold_error(const char* msg, ...) ATTRIBUTE_PRINTF_1;
93*a9fa9459Szrj 
94*a9fa9459Szrj // This function is called to issue a warning.
95*a9fa9459Szrj extern void
96*a9fa9459Szrj gold_warning(const char* msg, ...) ATTRIBUTE_PRINTF_1;
97*a9fa9459Szrj 
98*a9fa9459Szrj // This function is called to print an informational message.
99*a9fa9459Szrj extern void
100*a9fa9459Szrj gold_info(const char* msg, ...) ATTRIBUTE_PRINTF_1;
101*a9fa9459Szrj 
102*a9fa9459Szrj // This function is called to emit an error message and then
103*a9fa9459Szrj // immediately exit with fallback status (e.g., when
104*a9fa9459Szrj // --incremental-update fails and the link needs to be restarted
105*a9fa9459Szrj // with --incremental-full).
106*a9fa9459Szrj extern void
107*a9fa9459Szrj gold_fallback(const char* format, ...) ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF_1;
108*a9fa9459Szrj 
109*a9fa9459Szrj // Work around a bug in gcc 4.3.0.  http://gcc.gnu.org/PR35546 .  This
110*a9fa9459Szrj // can probably be removed after the bug has been fixed for a while.
111*a9fa9459Szrj #ifdef HAVE_TEMPLATE_ATTRIBUTES
112*a9fa9459Szrj #define TEMPLATE_ATTRIBUTE_PRINTF_4 ATTRIBUTE_PRINTF_4
113*a9fa9459Szrj #else
114*a9fa9459Szrj #define TEMPLATE_ATTRIBUTE_PRINTF_4
115*a9fa9459Szrj #endif
116*a9fa9459Szrj 
117*a9fa9459Szrj // This function is called to issue an error at the location of a
118*a9fa9459Szrj // reloc.
119*a9fa9459Szrj template<int size, bool big_endian>
120*a9fa9459Szrj extern void
121*a9fa9459Szrj gold_error_at_location(const Relocate_info<size, big_endian>*,
122*a9fa9459Szrj 		       size_t, off_t, const char* format, ...)
123*a9fa9459Szrj   TEMPLATE_ATTRIBUTE_PRINTF_4;
124*a9fa9459Szrj 
125*a9fa9459Szrj // This function is called to issue a warning at the location of a
126*a9fa9459Szrj // reloc.
127*a9fa9459Szrj template<int size, bool big_endian>
128*a9fa9459Szrj extern void
129*a9fa9459Szrj gold_warning_at_location(const Relocate_info<size, big_endian>*,
130*a9fa9459Szrj 			 size_t, off_t, const char* format, ...)
131*a9fa9459Szrj   TEMPLATE_ATTRIBUTE_PRINTF_4;
132*a9fa9459Szrj 
133*a9fa9459Szrj // This function is called to report an undefined symbol without
134*a9fa9459Szrj // a relocation (e.g., referenced by a dynamic object).  SYM is
135*a9fa9459Szrj // the undefined symbol.  The file name associated with the SYM
136*a9fa9459Szrj // is used to print a location for the undefined symbol.
137*a9fa9459Szrj extern void
138*a9fa9459Szrj gold_undefined_symbol(const Symbol*);
139*a9fa9459Szrj 
140*a9fa9459Szrj // This function is called to report an undefined symbol resulting
141*a9fa9459Szrj // from a relocation.  SYM is the undefined symbol.  RELINFO is the
142*a9fa9459Szrj // general relocation info.  RELNUM is the number of the reloc,
143*a9fa9459Szrj // and RELOFFSET is the reloc's offset.
144*a9fa9459Szrj template<int size, bool big_endian>
145*a9fa9459Szrj extern void
146*a9fa9459Szrj gold_undefined_symbol_at_location(const Symbol*,
147*a9fa9459Szrj 		                  const Relocate_info<size, big_endian>*,
148*a9fa9459Szrj 		                  size_t, off_t);
149*a9fa9459Szrj 
150*a9fa9459Szrj // This is function is called in some cases if we run out of memory.
151*a9fa9459Szrj extern void
152*a9fa9459Szrj gold_nomem() ATTRIBUTE_NORETURN;
153*a9fa9459Szrj 
154*a9fa9459Szrj // In versions of gcc before 4.3, using __FUNCTION__ in a template
155*a9fa9459Szrj // function can cause gcc to get confused about whether or not the
156*a9fa9459Szrj // function can return.  See http://gcc.gnu.org/PR30988.  Use a macro
157*a9fa9459Szrj // to avoid the problem.  This can be removed when we no longer need
158*a9fa9459Szrj // to care about gcc versions before 4.3.
159*a9fa9459Szrj #if defined(__GNUC__) && GCC_VERSION < 4003
160*a9fa9459Szrj #define FUNCTION_NAME static_cast<const char*>(__FUNCTION__)
161*a9fa9459Szrj #else
162*a9fa9459Szrj #define FUNCTION_NAME __FUNCTION__
163*a9fa9459Szrj #endif
164*a9fa9459Szrj 
165*a9fa9459Szrj // This macro and function are used in cases which can not arise if
166*a9fa9459Szrj // the code is written correctly.
167*a9fa9459Szrj 
168*a9fa9459Szrj #define gold_unreachable() \
169*a9fa9459Szrj   (gold::do_gold_unreachable(__FILE__, __LINE__, FUNCTION_NAME))
170*a9fa9459Szrj 
171*a9fa9459Szrj extern void do_gold_unreachable(const char*, int, const char*)
172*a9fa9459Szrj   ATTRIBUTE_NORETURN;
173*a9fa9459Szrj 
174*a9fa9459Szrj // Assertion check.
175*a9fa9459Szrj 
176*a9fa9459Szrj #define gold_assert(expr) ((void)(!(expr) ? gold_unreachable(), 0 : 0))
177*a9fa9459Szrj 
178*a9fa9459Szrj // Print version information.
179*a9fa9459Szrj extern void
180*a9fa9459Szrj print_version(bool print_short);
181*a9fa9459Szrj 
182*a9fa9459Szrj // Get the version string.
183*a9fa9459Szrj extern const char*
184*a9fa9459Szrj get_version_string();
185*a9fa9459Szrj 
186*a9fa9459Szrj // Convert numeric types without unnoticed loss of precision.
187*a9fa9459Szrj template<typename To, typename From>
188*a9fa9459Szrj inline To
convert_types(const From from)189*a9fa9459Szrj convert_types(const From from)
190*a9fa9459Szrj {
191*a9fa9459Szrj   To to = from;
192*a9fa9459Szrj   gold_assert(static_cast<From>(to) == from);
193*a9fa9459Szrj   return to;
194*a9fa9459Szrj }
195*a9fa9459Szrj 
196*a9fa9459Szrj // A common case of convert_types<>: convert to section_size_type.
197*a9fa9459Szrj template<typename From>
198*a9fa9459Szrj inline section_size_type
convert_to_section_size_type(const From from)199*a9fa9459Szrj convert_to_section_size_type(const From from)
200*a9fa9459Szrj { return convert_types<section_size_type, From>(from); }
201*a9fa9459Szrj 
202*a9fa9459Szrj // Queue up the first set of tasks.
203*a9fa9459Szrj extern void
204*a9fa9459Szrj queue_initial_tasks(const General_options&,
205*a9fa9459Szrj 		    Dirsearch&,
206*a9fa9459Szrj 		    const Command_line&,
207*a9fa9459Szrj 		    Workqueue*,
208*a9fa9459Szrj 		    Input_objects*,
209*a9fa9459Szrj 		    Symbol_table*,
210*a9fa9459Szrj 		    Layout*,
211*a9fa9459Szrj 		    Mapfile*);
212*a9fa9459Szrj 
213*a9fa9459Szrj // Queue up the set of tasks to be done before
214*a9fa9459Szrj // the middle set of tasks.  Only used when garbage
215*a9fa9459Szrj // collection is to be done.
216*a9fa9459Szrj extern void
217*a9fa9459Szrj queue_middle_gc_tasks(const General_options&,
218*a9fa9459Szrj                       const Task*,
219*a9fa9459Szrj                       const Input_objects*,
220*a9fa9459Szrj                       Symbol_table*,
221*a9fa9459Szrj                       Layout*,
222*a9fa9459Szrj                       Workqueue*,
223*a9fa9459Szrj                       Mapfile*);
224*a9fa9459Szrj 
225*a9fa9459Szrj // Queue up the middle set of tasks.
226*a9fa9459Szrj extern void
227*a9fa9459Szrj queue_middle_tasks(const General_options&,
228*a9fa9459Szrj 		   const Task*,
229*a9fa9459Szrj 		   const Input_objects*,
230*a9fa9459Szrj 		   Symbol_table*,
231*a9fa9459Szrj 		   Layout*,
232*a9fa9459Szrj 		   Workqueue*,
233*a9fa9459Szrj 		   Mapfile*);
234*a9fa9459Szrj 
235*a9fa9459Szrj // Queue up the final set of tasks.
236*a9fa9459Szrj extern void
237*a9fa9459Szrj queue_final_tasks(const General_options&,
238*a9fa9459Szrj 		  const Input_objects*,
239*a9fa9459Szrj 		  const Symbol_table*,
240*a9fa9459Szrj 		  Layout*,
241*a9fa9459Szrj 		  Workqueue*,
242*a9fa9459Szrj 		  Output_file* of);
243*a9fa9459Szrj 
244*a9fa9459Szrj inline bool
is_prefix_of(const char * prefix,const char * str)245*a9fa9459Szrj is_prefix_of(const char* prefix, const char* str)
246*a9fa9459Szrj {
247*a9fa9459Szrj   return strncmp(prefix, str, strlen(prefix)) == 0;
248*a9fa9459Szrj }
249*a9fa9459Szrj 
250*a9fa9459Szrj const char* const cident_section_start_prefix = "__start_";
251*a9fa9459Szrj const char* const cident_section_stop_prefix = "__stop_";
252*a9fa9459Szrj 
253*a9fa9459Szrj // Returns true if the name is a valid C identifier
254*a9fa9459Szrj inline bool
is_cident(const char * name)255*a9fa9459Szrj is_cident(const char* name)
256*a9fa9459Szrj {
257*a9fa9459Szrj   return (name[strspn(name,
258*a9fa9459Szrj 		      ("0123456789"
259*a9fa9459Szrj 		       "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
260*a9fa9459Szrj 		       "abcdefghijklmnopqrstuvwxyz"
261*a9fa9459Szrj 		       "_"))]
262*a9fa9459Szrj 	  == '\0');
263*a9fa9459Szrj }
264*a9fa9459Szrj 
265*a9fa9459Szrj // We sometimes need to hash strings.  Ideally we should use std::tr1::hash or
266*a9fa9459Szrj // __gnu_cxx::hash on some systems but there is no guarantee that either
267*a9fa9459Szrj // one is available.  For portability, we define simple string hash functions.
268*a9fa9459Szrj 
269*a9fa9459Szrj template<typename Char_type>
270*a9fa9459Szrj inline size_t
string_hash(const Char_type * s,size_t length)271*a9fa9459Szrj string_hash(const Char_type* s, size_t length)
272*a9fa9459Szrj {
273*a9fa9459Szrj   // This is the hash function used by the dynamic linker for
274*a9fa9459Szrj   // DT_GNU_HASH entries.  I compared this to a Fowler/Noll/Vo hash
275*a9fa9459Szrj   // for a C++ program with 385,775 global symbols.  This hash
276*a9fa9459Szrj   // function was very slightly worse.  However, it is much faster to
277*a9fa9459Szrj   // compute.  Overall wall clock time was a win.
278*a9fa9459Szrj   const unsigned char* p = reinterpret_cast<const unsigned char*>(s);
279*a9fa9459Szrj   size_t h = 5381;
280*a9fa9459Szrj   for (size_t i = 0; i < length * sizeof(Char_type); ++i)
281*a9fa9459Szrj     h = h * 33 + *p++;
282*a9fa9459Szrj   return h;
283*a9fa9459Szrj }
284*a9fa9459Szrj 
285*a9fa9459Szrj // Same as above except we expect the string to be zero terminated.
286*a9fa9459Szrj 
287*a9fa9459Szrj template<typename Char_type>
288*a9fa9459Szrj inline size_t
string_hash(const Char_type * s)289*a9fa9459Szrj string_hash(const Char_type* s)
290*a9fa9459Szrj {
291*a9fa9459Szrj   const unsigned char* p = reinterpret_cast<const unsigned char*>(s);
292*a9fa9459Szrj   size_t h = 5381;
293*a9fa9459Szrj   for (size_t i = 0; s[i] != 0; ++i)
294*a9fa9459Szrj     {
295*a9fa9459Szrj       for (size_t j = 0; j < sizeof(Char_type); j++)
296*a9fa9459Szrj 	h = h * 33 + *p++;
297*a9fa9459Szrj     }
298*a9fa9459Szrj 
299*a9fa9459Szrj   return h;
300*a9fa9459Szrj }
301*a9fa9459Szrj 
302*a9fa9459Szrj // Return whether STRING contains a wildcard character.  This is used
303*a9fa9459Szrj // to speed up matching.
304*a9fa9459Szrj 
305*a9fa9459Szrj inline bool
is_wildcard_string(const char * s)306*a9fa9459Szrj is_wildcard_string(const char* s)
307*a9fa9459Szrj {
308*a9fa9459Szrj   return strpbrk(s, "?*[") != NULL;
309*a9fa9459Szrj }
310*a9fa9459Szrj 
311*a9fa9459Szrj } // End namespace gold.
312*a9fa9459Szrj 
313*a9fa9459Szrj #endif // !defined(GOLD_GOLD_H)
314