| 1 | // options.h -- handle command line options for gold -*- C++ -*- |
| 2 | |
| 3 | // Command_line |
| 4 | // Holds everything we get from the command line. |
| 5 | // General_options (from Command_line::options()) |
| 6 | // Options which are not position dependent. |
| 7 | // Input_argument (from Command_line::inputs()) |
| 8 | // The list of input files, including -l options. |
| 9 | // Position_dependent_options (from Input_argument::options()) |
| 10 | // Position dependent options which apply to this argument. |
| 11 | |
| 12 | #ifndef GOLD_OPTIONS_H |
| 13 | #define GOLD_OPTIONS_H |
| 14 | |
| 15 | #include <list> |
| 16 | #include <string> |
| 17 | #include <vector> |
| 18 | |
| 19 | namespace gold |
| 20 | { |
| 21 | |
| 22 | class Command_line; |
| 23 | class Input_file_group; |
| 24 | |
| 25 | namespace options { |
| 26 | |
| 27 | class Command_line_options; |
| 28 | struct One_option; |
| 29 | |
| 30 | } // End namespace gold::options. |
| 31 | |
| 32 | // The position independent options which apply to the whole link. |
| 33 | // There are a lot of them. |
| 34 | |
| 35 | class General_options |
| 36 | { |
| 37 | public: |
| 38 | General_options(); |
| 39 | |
| 40 | // -I: dynamic linker name. |
| 41 | const char* |
| 42 | dynamic_linker() const |
| 43 | { return this->dynamic_linker_; } |
| 44 | |
| 45 | // -L: Library search path. |
| 46 | typedef std::list<const char*> Dir_list; |
| 47 | |
| 48 | const Dir_list& |
| 49 | search_path() const |
| 50 | { return this->search_path_; } |
| 51 | |
| 52 | // -o: Output file name. |
| 53 | const char* |
| 54 | output_file_name() const |
| 55 | { return this->output_file_name_; } |
| 56 | |
| 57 | // -r: Whether we are doing a relocatable link. |
| 58 | bool |
| 59 | is_relocatable() const |
| 60 | { return this->is_relocatable_; } |
| 61 | |
| 62 | // --shared: Whether generating a shared object. |
| 63 | bool |
| 64 | is_shared() const |
| 65 | { return this->is_shared_; } |
| 66 | |
| 67 | // --static: Whether doing a static link. |
| 68 | bool |
| 69 | is_static() const |
| 70 | { return this->is_static_; } |
| 71 | |
| 72 | private: |
| 73 | // Don't copy this structure. |
| 74 | General_options(const General_options&); |
| 75 | General_options& operator=(const General_options&); |
| 76 | |
| 77 | friend class Command_line; |
| 78 | friend class options::Command_line_options; |
| 79 | |
| 80 | void |
| 81 | set_dynamic_linker(const char* arg) |
| 82 | { this->dynamic_linker_ = arg; } |
| 83 | |
| 84 | void |
| 85 | add_to_search_path(const char* arg) |
| 86 | { this->search_path_.push_back(arg); } |
| 87 | |
| 88 | void |
| 89 | set_output_file_name(const char* arg) |
| 90 | { this->output_file_name_ = arg; } |
| 91 | |
| 92 | void |
| 93 | set_relocatable() |
| 94 | { this->is_relocatable_ = true; } |
| 95 | |
| 96 | void |
| 97 | set_shared() |
| 98 | { this->is_shared_ = true; } |
| 99 | |
| 100 | void |
| 101 | set_static() |
| 102 | { this->is_static_ = true; } |
| 103 | |
| 104 | void |
| 105 | ignore(const char*) |
| 106 | { } |
| 107 | |
| 108 | const char* dynamic_linker_; |
| 109 | Dir_list search_path_; |
| 110 | const char* output_file_name_; |
| 111 | bool is_relocatable_; |
| 112 | bool is_shared_; |
| 113 | bool is_static_; |
| 114 | }; |
| 115 | |
| 116 | // The current state of the position dependent options. |
| 117 | |
| 118 | class Position_dependent_options |
| 119 | { |
| 120 | public: |
| 121 | Position_dependent_options(); |
| 122 | |
| 123 | // -Bstatic: Whether we are searching for a static archive rather |
| 124 | // than a shared object. |
| 125 | bool |
| 126 | do_static_search() const |
| 127 | { return this->do_static_search_; } |
| 128 | |
| 129 | // --as-needed: Whether to add a DT_NEEDED argument only if the |
| 130 | // dynamic object is used. |
| 131 | bool |
| 132 | as_needed() const |
| 133 | { return this->as_needed_; } |
| 134 | |
| 135 | void |
| 136 | set_static_search() |
| 137 | { this->do_static_search_ = true; } |
| 138 | |
| 139 | void |
| 140 | set_dynamic_search() |
| 141 | { this->do_static_search_ = false; } |
| 142 | |
| 143 | void |
| 144 | set_as_needed() |
| 145 | { this->as_needed_ = true; } |
| 146 | |
| 147 | void |
| 148 | clear_as_needed() |
| 149 | { this->as_needed_ = false; } |
| 150 | |
| 151 | private: |
| 152 | bool do_static_search_; |
| 153 | bool as_needed_; |
| 154 | }; |
| 155 | |
| 156 | // A single file or library argument from the command line. |
| 157 | |
| 158 | class Input_file_argument |
| 159 | { |
| 160 | public: |
| 161 | Input_file_argument() |
| 162 | : name_(), is_lib_(false), options_() |
| 163 | { } |
| 164 | |
| 165 | Input_file_argument(const char* name, bool is_lib, |
| 166 | const Position_dependent_options& options) |
| 167 | : name_(name), is_lib_(is_lib), options_(options) |
| 168 | { } |
| 169 | |
| 170 | const char* |
| 171 | name() const |
| 172 | { return this->name_.c_str(); } |
| 173 | |
| 174 | const Position_dependent_options& |
| 175 | options() const |
| 176 | { return this->options_; } |
| 177 | |
| 178 | bool |
| 179 | is_lib() const |
| 180 | { return this->is_lib_; } |
| 181 | |
| 182 | private: |
| 183 | // We use std::string, not const char*, here for convenience when |
| 184 | // using script files, so that we do not have to preserve the string |
| 185 | // in that case. |
| 186 | std::string name_; |
| 187 | bool is_lib_; |
| 188 | Position_dependent_options options_; |
| 189 | }; |
| 190 | |
| 191 | // A file or library, or a group, from the command line. |
| 192 | |
| 193 | class Input_argument |
| 194 | { |
| 195 | public: |
| 196 | // Create a file or library argument. |
| 197 | explicit Input_argument(Input_file_argument file) |
| 198 | : is_file_(true), file_(file), group_(NULL) |
| 199 | { } |
| 200 | |
| 201 | // Create a group argument. |
| 202 | explicit Input_argument(Input_file_group* group) |
| 203 | : is_file_(false), group_(group) |
| 204 | { } |
| 205 | |
| 206 | // Return whether this is a file. |
| 207 | bool |
| 208 | is_file() const |
| 209 | { return this->is_file_; } |
| 210 | |
| 211 | // Return whether this is a group. |
| 212 | bool |
| 213 | is_group() const |
| 214 | { return !this->is_file_; } |
| 215 | |
| 216 | // Return the information about the file. |
| 217 | const Input_file_argument& |
| 218 | file() const |
| 219 | { |
| 220 | gold_assert(this->is_file_); |
| 221 | return this->file_; |
| 222 | } |
| 223 | |
| 224 | // Return the information about the group. |
| 225 | const Input_file_group* |
| 226 | group() const |
| 227 | { |
| 228 | gold_assert(!this->is_file_); |
| 229 | return this->group_; |
| 230 | } |
| 231 | |
| 232 | Input_file_group* |
| 233 | group() |
| 234 | { |
| 235 | gold_assert(!this->is_file_); |
| 236 | return this->group_; |
| 237 | } |
| 238 | |
| 239 | private: |
| 240 | bool is_file_; |
| 241 | Input_file_argument file_; |
| 242 | Input_file_group* group_; |
| 243 | }; |
| 244 | |
| 245 | // A group from the command line. This is a set of arguments within |
| 246 | // --start-group ... --end-group. |
| 247 | |
| 248 | class Input_file_group |
| 249 | { |
| 250 | public: |
| 251 | typedef std::vector<Input_argument> Files; |
| 252 | typedef Files::const_iterator const_iterator; |
| 253 | |
| 254 | Input_file_group() |
| 255 | : files_() |
| 256 | { } |
| 257 | |
| 258 | // Add a file to the end of the group. |
| 259 | void |
| 260 | add_file(const Input_file_argument& arg) |
| 261 | { this->files_.push_back(Input_argument(arg)); } |
| 262 | |
| 263 | // Iterators to iterate over the group contents. |
| 264 | |
| 265 | const_iterator |
| 266 | begin() const |
| 267 | { return this->files_.begin(); } |
| 268 | |
| 269 | const_iterator |
| 270 | end() const |
| 271 | { return this->files_.end(); } |
| 272 | |
| 273 | private: |
| 274 | Files files_; |
| 275 | }; |
| 276 | |
| 277 | // A list of files from the command line or a script. |
| 278 | |
| 279 | class Input_arguments |
| 280 | { |
| 281 | public: |
| 282 | typedef std::vector<Input_argument> Input_argument_list; |
| 283 | typedef Input_argument_list::const_iterator const_iterator; |
| 284 | |
| 285 | Input_arguments() |
| 286 | : input_argument_list_(), in_group_(false) |
| 287 | { } |
| 288 | |
| 289 | // Add a file. |
| 290 | void |
| 291 | add_file(const Input_file_argument& arg); |
| 292 | |
| 293 | // Start a group (the --start-group option). |
| 294 | void |
| 295 | start_group(); |
| 296 | |
| 297 | // End a group (the --end-group option). |
| 298 | void |
| 299 | end_group(); |
| 300 | |
| 301 | // Return whether we are currently in a group. |
| 302 | bool |
| 303 | in_group() const |
| 304 | { return this->in_group_; } |
| 305 | |
| 306 | // Iterators to iterate over the list of input files. |
| 307 | |
| 308 | const_iterator |
| 309 | begin() const |
| 310 | { return this->input_argument_list_.begin(); } |
| 311 | |
| 312 | const_iterator |
| 313 | end() const |
| 314 | { return this->input_argument_list_.end(); } |
| 315 | |
| 316 | // Return whether the list is empty. |
| 317 | bool |
| 318 | empty() const |
| 319 | { return this->input_argument_list_.empty(); } |
| 320 | |
| 321 | private: |
| 322 | Input_argument_list input_argument_list_; |
| 323 | bool in_group_; |
| 324 | }; |
| 325 | |
| 326 | // All the information read from the command line. |
| 327 | |
| 328 | class Command_line |
| 329 | { |
| 330 | public: |
| 331 | typedef Input_arguments::const_iterator const_iterator; |
| 332 | |
| 333 | Command_line(); |
| 334 | |
| 335 | // Process the command line options. This will exit with an |
| 336 | // appropriate error message if an unrecognized option is seen. |
| 337 | void |
| 338 | process(int argc, char** argv); |
| 339 | |
| 340 | // Handle a -l option. |
| 341 | int |
| 342 | process_l_option(int, char**, char*); |
| 343 | |
| 344 | // Handle a --start-group option. |
| 345 | void |
| 346 | start_group(const char* arg); |
| 347 | |
| 348 | // Handle a --end-group option. |
| 349 | void |
| 350 | end_group(const char* arg); |
| 351 | |
| 352 | // Get the general options. |
| 353 | const General_options& |
| 354 | options() const |
| 355 | { return this->options_; } |
| 356 | |
| 357 | // Iterators to iterate over the list of input files. |
| 358 | |
| 359 | const_iterator |
| 360 | begin() const |
| 361 | { return this->inputs_.begin(); } |
| 362 | |
| 363 | const_iterator |
| 364 | end() const |
| 365 | { return this->inputs_.end(); } |
| 366 | |
| 367 | private: |
| 368 | Command_line(const Command_line&); |
| 369 | Command_line& operator=(const Command_line&); |
| 370 | |
| 371 | // Report usage error. |
| 372 | void |
| 373 | usage() ATTRIBUTE_NORETURN; |
| 374 | void |
| 375 | usage(const char* msg, const char* opt) ATTRIBUTE_NORETURN; |
| 376 | void |
| 377 | usage(const char* msg, char opt) ATTRIBUTE_NORETURN; |
| 378 | |
| 379 | // Apply a command line option. |
| 380 | void |
| 381 | apply_option(const gold::options::One_option&, const char*); |
| 382 | |
| 383 | // Add a file. |
| 384 | void |
| 385 | add_file(const char* name, bool is_lib); |
| 386 | |
| 387 | General_options options_; |
| 388 | Position_dependent_options position_options_; |
| 389 | Input_arguments inputs_; |
| 390 | }; |
| 391 | |
| 392 | } // End namespace gold. |
| 393 | |
| 394 | #endif // !defined(GOLD_OPTIONS_H) |