[ec24a37] | 1 | /* |
---|
[977c3de] | 2 | * Copyright (c) 2011, Chris Johns <chrisj@rtems.org> |
---|
[ec24a37] | 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. |
---|
[977c3de] | 7 | * |
---|
[ec24a37] | 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 file manages access the image contained in various file |
---|
| 22 | * formats. |
---|
| 23 | * |
---|
| 24 | * The base element is a file. It references a object file that is either |
---|
| 25 | * inside an archive or stand alone. You access a object file by constructing a |
---|
| 26 | * handle. A handle is the object file with the specific file descriptor |
---|
| 27 | * created when the archive or object file was opened. |
---|
| 28 | * |
---|
[977c3de] | 29 | * |
---|
[ec24a37] | 30 | */ |
---|
| 31 | |
---|
| 32 | #if !defined (_RLD_FILES_H_) |
---|
| 33 | #define _RLD_FILES_H_ |
---|
| 34 | |
---|
| 35 | #include <list> |
---|
| 36 | #include <map> |
---|
| 37 | #include <string> |
---|
| 38 | #include <vector> |
---|
| 39 | |
---|
| 40 | #include <rld.h> |
---|
| 41 | |
---|
| 42 | namespace rld |
---|
| 43 | { |
---|
| 44 | namespace files |
---|
| 45 | { |
---|
| 46 | /** |
---|
| 47 | * Container of file paths. |
---|
| 48 | */ |
---|
| 49 | typedef std::vector < std::string > paths; |
---|
| 50 | |
---|
| 51 | /** |
---|
| 52 | * Container of files. |
---|
| 53 | */ |
---|
| 54 | typedef std::vector < file > files; |
---|
| 55 | |
---|
| 56 | /** |
---|
| 57 | * Container of archive files. |
---|
| 58 | */ |
---|
[544de91] | 59 | typedef std::map < const std::string, archive* > archives; |
---|
[ec24a37] | 60 | |
---|
| 61 | /** |
---|
| 62 | * Container of object files. |
---|
| 63 | */ |
---|
[544de91] | 64 | typedef std::map < const std::string, object* > objects; |
---|
[ec24a37] | 65 | |
---|
| 66 | /** |
---|
| 67 | * Container list of object files. |
---|
| 68 | */ |
---|
| 69 | typedef std::list < object* > object_list; |
---|
[977c3de] | 70 | |
---|
[fd8a2c5] | 71 | /** |
---|
| 72 | * Return the basename of the file name. |
---|
| 73 | * |
---|
| 74 | * @param name The full file name. |
---|
| 75 | * @return std::string The basename of the file. |
---|
| 76 | */ |
---|
| 77 | std::string basename (const std::string& name); |
---|
| 78 | |
---|
| 79 | /** |
---|
| 80 | * Return the dirname of the file name. |
---|
| 81 | * |
---|
| 82 | * @param name The full file name. |
---|
| 83 | * @return std::string The dirname of the file. |
---|
| 84 | */ |
---|
| 85 | std::string dirname (const std::string& name); |
---|
| 86 | |
---|
| 87 | /** |
---|
| 88 | * Return the extension of the file name. |
---|
| 89 | * |
---|
| 90 | * @param name The full file name. |
---|
| 91 | * @return std::string The extension of the file. |
---|
| 92 | */ |
---|
| 93 | std::string extension (const std::string& name); |
---|
| 94 | |
---|
[ec24a37] | 95 | /** |
---|
| 96 | * Split a path from a string with a delimiter to the path container. Add |
---|
| 97 | * only the paths that exist and ignore those that do not. |
---|
[fd8a2c5] | 98 | * |
---|
| 99 | * @param path The paths as a single string delimited by the path |
---|
| 100 | * separator. |
---|
| 101 | * @param paths The split path paths. |
---|
[ec24a37] | 102 | */ |
---|
| 103 | void path_split (const std::string& path, |
---|
[a5fcdd5] | 104 | paths& paths); |
---|
[ec24a37] | 105 | |
---|
| 106 | /** |
---|
| 107 | * Make a path by joining the parts with required separator. |
---|
[2bcaf0c] | 108 | * |
---|
| 109 | * @param path_ The path component to be joined. |
---|
| 110 | * @param file_ The file name to add to the path. |
---|
| 111 | * @param joined The joined path and file name with a path separator. |
---|
[ec24a37] | 112 | */ |
---|
[977c3de] | 113 | void path_join (const std::string& path_, |
---|
| 114 | const std::string& file_, |
---|
[2bcaf0c] | 115 | std::string& joined); |
---|
[ec24a37] | 116 | |
---|
| 117 | /** |
---|
[2bcaf0c] | 118 | * Check the path is a file using a stat call. |
---|
| 119 | * |
---|
| 120 | * @param path The path to check. |
---|
| 121 | * @retval true The path is valid. |
---|
| 122 | * @retval false The path is not valid. |
---|
[ec24a37] | 123 | */ |
---|
| 124 | bool check_file (const std::string& path); |
---|
| 125 | |
---|
| 126 | /** |
---|
[2bcaf0c] | 127 | * Check if the path is a directory. |
---|
| 128 | * |
---|
| 129 | * @param path The path to check. |
---|
| 130 | * @retval false The path is not a directory. |
---|
| 131 | * @retval true The path is a directory. |
---|
[ec24a37] | 132 | */ |
---|
| 133 | bool check_directory (const std::string& path); |
---|
[977c3de] | 134 | |
---|
[ec24a37] | 135 | /** |
---|
| 136 | * Find the file given a container of paths and file names. |
---|
| 137 | * |
---|
| 138 | * @param path The path of the file if found else empty. |
---|
| 139 | * @param name The name of the file to search for. |
---|
| 140 | * @param search_paths The container of paths to search. |
---|
| 141 | */ |
---|
[977c3de] | 142 | void find_file (std::string& path, |
---|
[ec24a37] | 143 | const std::string& name, |
---|
| 144 | paths& search_paths); |
---|
| 145 | |
---|
| 146 | /** |
---|
[2bcaf0c] | 147 | * A file is a single object file that is either in an @ref archive or |
---|
| 148 | * a separate stand alone @ref object file. |
---|
[ec24a37] | 149 | */ |
---|
| 150 | class file |
---|
| 151 | { |
---|
| 152 | public: |
---|
| 153 | /** |
---|
| 154 | * Construct the file from the component parts when part of an archive. |
---|
| 155 | * |
---|
| 156 | * @param aname The archive name. |
---|
| 157 | * @param oname The object file name. |
---|
| 158 | * @param offset The offset in the archive the object file starts. |
---|
| 159 | * @param size The size of the archive the object file starts. |
---|
| 160 | */ |
---|
| 161 | file (const std::string& aname, |
---|
| 162 | const std::string& oname, |
---|
| 163 | off_t offset, |
---|
| 164 | size_t size); |
---|
| 165 | |
---|
| 166 | /** |
---|
| 167 | * Construct the name by splitting the full path into an archive, |
---|
| 168 | * object file name and offset. |
---|
| 169 | * |
---|
| 170 | * @param path The path to the image. |
---|
| 171 | * @param is_object If true (default) the name is for an object file. |
---|
| 172 | */ |
---|
| 173 | file (const std::string& path, bool is_object = true); |
---|
| 174 | |
---|
| 175 | /** |
---|
| 176 | * Contruct an empty file. |
---|
| 177 | */ |
---|
| 178 | file (); |
---|
| 179 | |
---|
| 180 | /** |
---|
| 181 | * Set a name from the path. |
---|
| 182 | * |
---|
| 183 | * @param path The path to the image. |
---|
| 184 | * @param is_object If true (default) the name is for an object file. |
---|
| 185 | */ |
---|
| 186 | void set (const std::string& path, bool is_object = true); |
---|
| 187 | |
---|
| 188 | /** |
---|
| 189 | * Is an archive returns true if the file is in an archive. |
---|
| 190 | * |
---|
| 191 | * @retval true The object file is in an archive. |
---|
| 192 | * @retval false The object file is stand alone. |
---|
| 193 | */ |
---|
| 194 | bool is_archive () const; |
---|
| 195 | |
---|
| 196 | /** |
---|
| 197 | * Is object file stand alone. |
---|
| 198 | * |
---|
| 199 | * @retval true The object file is stand alone. |
---|
| 200 | * @retval false The object could be part of an archive. |
---|
| 201 | */ |
---|
| 202 | bool is_object () const; |
---|
| 203 | |
---|
| 204 | /** |
---|
| 205 | * Valid returns true if there is a valid name. |
---|
| 206 | * |
---|
| 207 | * @retval true There is a valid name. |
---|
| 208 | * @retval false There is not name assigned. |
---|
| 209 | */ |
---|
| 210 | bool is_valid () const; |
---|
| 211 | |
---|
| 212 | /** |
---|
| 213 | * Exists returns true if the archive or object file is present on disk |
---|
| 214 | * and a regular file. |
---|
| 215 | * |
---|
| 216 | * @retval true The file is valid and a regular file. |
---|
| 217 | * @retval false The file is either not present, not accessable or not a |
---|
| 218 | * regular file. |
---|
| 219 | */ |
---|
| 220 | bool exists () const; |
---|
| 221 | |
---|
| 222 | /** |
---|
| 223 | * The path maps to the real file on disk. The file may not be valid. |
---|
| 224 | * |
---|
| 225 | * @return const std::string The real path to the file on disk. |
---|
| 226 | */ |
---|
| 227 | const std::string path () const; |
---|
| 228 | |
---|
| 229 | /** |
---|
| 230 | * The full path. |
---|
| 231 | * |
---|
| 232 | * @return const std::string The full path to the image. |
---|
| 233 | */ |
---|
| 234 | const std::string full () const; |
---|
| 235 | |
---|
| 236 | /** |
---|
| 237 | * The base path. It is the basename of the full path. |
---|
| 238 | * |
---|
| 239 | * @return const std::string The basename of the full path to the image. |
---|
| 240 | */ |
---|
| 241 | const std::string basename () const; |
---|
| 242 | |
---|
| 243 | /** |
---|
| 244 | * The archive name component. A length of 0 means there was not |
---|
| 245 | * archive component. |
---|
[2bcaf0c] | 246 | * |
---|
| 247 | * @return const std::string& The archive name. |
---|
[ec24a37] | 248 | */ |
---|
| 249 | const std::string& aname () const; |
---|
| 250 | |
---|
| 251 | /** |
---|
| 252 | * The object name. There is always an object name. |
---|
[2bcaf0c] | 253 | * |
---|
| 254 | * @return const std::string& The object name. |
---|
[ec24a37] | 255 | */ |
---|
| 256 | const std::string& oname () const; |
---|
| 257 | |
---|
| 258 | /** |
---|
[fe19d06] | 259 | * The object's offset in the archive or on disk. |
---|
[2bcaf0c] | 260 | * |
---|
| 261 | * @return off_t The offset part of the file name. |
---|
[ec24a37] | 262 | */ |
---|
| 263 | off_t offset () const; |
---|
| 264 | |
---|
| 265 | /** |
---|
| 266 | * The object's size in the archive. |
---|
[2bcaf0c] | 267 | * |
---|
| 268 | * @return size_t The size of the file in bytes. |
---|
[ec24a37] | 269 | */ |
---|
| 270 | size_t size () const; |
---|
| 271 | |
---|
| 272 | private: |
---|
| 273 | std::string aname_; //< The archive name. |
---|
| 274 | std::string oname_; //< The object name. |
---|
| 275 | off_t offset_; //< The object's offset in the archive. |
---|
[fe19d06] | 276 | size_t size_; //< The object's size in the archive or on disk. |
---|
[ec24a37] | 277 | }; |
---|
| 278 | |
---|
| 279 | /** |
---|
[2bcaf0c] | 280 | * Image is the base file type and lets us have a single container to hold |
---|
| 281 | * the types of images we need to support. |
---|
[ec24a37] | 282 | */ |
---|
| 283 | class image |
---|
| 284 | { |
---|
| 285 | public: |
---|
| 286 | /** |
---|
| 287 | * Construct the image. |
---|
| 288 | * |
---|
| 289 | * @param name The name of the image. |
---|
| 290 | */ |
---|
| 291 | image (file& name); |
---|
| 292 | |
---|
| 293 | /** |
---|
| 294 | * Construct the image. |
---|
| 295 | * |
---|
[e78e2b0] | 296 | * @param path The file path. |
---|
[ec24a37] | 297 | * @param is_object If true (default) the name is for an object file. |
---|
| 298 | */ |
---|
| 299 | image (const std::string& path, bool is_object = true); |
---|
| 300 | |
---|
| 301 | /** |
---|
| 302 | * Construct the image. |
---|
| 303 | */ |
---|
| 304 | image (); |
---|
| 305 | |
---|
| 306 | /** |
---|
| 307 | * Destruct the image. |
---|
| 308 | */ |
---|
| 309 | virtual ~image (); |
---|
| 310 | |
---|
| 311 | /** |
---|
| 312 | * Open the image. You can open the image more than once but you need to |
---|
| 313 | * close it the same number of times. |
---|
[2bcaf0c] | 314 | * |
---|
| 315 | * @param name The @ref file name. |
---|
[ec24a37] | 316 | */ |
---|
| 317 | virtual void open (file& name); |
---|
| 318 | |
---|
| 319 | /** |
---|
| 320 | * Open the image. You can open the image more than once but you need to |
---|
| 321 | * close it the same number of times. |
---|
[2bcaf0c] | 322 | * |
---|
| 323 | * @param writeable If true the image is open as writable. The default is |
---|
| 324 | * false. |
---|
[ec24a37] | 325 | */ |
---|
| 326 | virtual void open (bool writable = false); |
---|
| 327 | |
---|
| 328 | /** |
---|
| 329 | * Close the image. |
---|
| 330 | */ |
---|
| 331 | virtual void close (); |
---|
| 332 | |
---|
| 333 | /** |
---|
| 334 | * Read a block from the file. |
---|
[2bcaf0c] | 335 | * |
---|
| 336 | * @param buffer The buffer to read the data into. |
---|
| 337 | * @param size The amount of data to read. |
---|
| 338 | * @return ssize_t The amount of data read. |
---|
[ec24a37] | 339 | */ |
---|
[fe19d06] | 340 | virtual ssize_t read (void* buffer, size_t size); |
---|
[ec24a37] | 341 | |
---|
| 342 | /** |
---|
| 343 | * Write a block from the file. |
---|
[2bcaf0c] | 344 | * |
---|
| 345 | * @param buffer The buffer of data to write. |
---|
| 346 | * @param size The amount of data to write. |
---|
| 347 | * @return ssize_t The amount of data written. |
---|
[ec24a37] | 348 | */ |
---|
| 349 | virtual ssize_t write (const void* buffer, size_t size); |
---|
| 350 | |
---|
| 351 | /** |
---|
[2bcaf0c] | 352 | * Seek to the offset in the image. |
---|
| 353 | * |
---|
| 354 | * @param offset The offset to seek too. |
---|
[ec24a37] | 355 | */ |
---|
| 356 | virtual void seek (off_t offset); |
---|
| 357 | |
---|
| 358 | /** |
---|
[2bcaf0c] | 359 | * Seek and then read. |
---|
| 360 | * |
---|
| 361 | * @param offset The offset to seek too before reading. |
---|
| 362 | * @param buffer The buffer to read the data into. |
---|
| 363 | * @param size The amount of data to read. |
---|
| 364 | * @retval true The data requested was read. |
---|
| 365 | * @retval false The request data was not read. |
---|
[ec24a37] | 366 | */ |
---|
| 367 | virtual bool seek_read (off_t offset, uint8_t* buffer, size_t size); |
---|
| 368 | |
---|
| 369 | /** |
---|
[2bcaf0c] | 370 | * Seek and then write. |
---|
| 371 | * |
---|
| 372 | * @param offset The offset to seek too before writing. |
---|
| 373 | * @param buffer The buffer of data to write. |
---|
| 374 | * @param size The amount of data to write. |
---|
| 375 | * @retval true The data requested was written. |
---|
| 376 | * @retval false The request data was not written. |
---|
[ec24a37] | 377 | */ |
---|
| 378 | virtual bool seek_write (off_t offset, const void* buffer, size_t size); |
---|
| 379 | |
---|
| 380 | /** |
---|
| 381 | * The name of the image. |
---|
[2bcaf0c] | 382 | * |
---|
| 383 | * @param const file& The @ref file name of the image. |
---|
[ec24a37] | 384 | */ |
---|
| 385 | const file& name () const; |
---|
| 386 | |
---|
| 387 | /** |
---|
| 388 | * References to the image. |
---|
[2bcaf0c] | 389 | * |
---|
| 390 | * @return int The number of references the image has. |
---|
[ec24a37] | 391 | */ |
---|
| 392 | virtual int references () const; |
---|
| 393 | |
---|
| 394 | /** |
---|
| 395 | * The file size. |
---|
[2bcaf0c] | 396 | * |
---|
| 397 | * @return size_t The size of the image. |
---|
[ec24a37] | 398 | */ |
---|
| 399 | virtual size_t size () const; |
---|
| 400 | |
---|
| 401 | /** |
---|
| 402 | * The file descriptor. |
---|
[2bcaf0c] | 403 | * |
---|
| 404 | * @return int The operating system file descriptor handle. |
---|
[ec24a37] | 405 | */ |
---|
| 406 | virtual int fd () const; |
---|
| 407 | |
---|
| 408 | /** |
---|
[977c3de] | 409 | * The ELF reference. |
---|
[2bcaf0c] | 410 | * |
---|
| 411 | * @return elf::file& The @ref elf::file object of the image. |
---|
[ec24a37] | 412 | */ |
---|
[977c3de] | 413 | elf::file& elf (); |
---|
[ec24a37] | 414 | |
---|
| 415 | /** |
---|
| 416 | * A symbol in the image has been referenced. |
---|
| 417 | */ |
---|
| 418 | virtual void symbol_referenced (); |
---|
| 419 | |
---|
| 420 | /** |
---|
| 421 | * Return the number of symbol references. |
---|
[2bcaf0c] | 422 | * |
---|
| 423 | * @return int The symbol references count. |
---|
[ec24a37] | 424 | */ |
---|
| 425 | virtual int symbol_references () const; |
---|
| 426 | |
---|
| 427 | /** |
---|
| 428 | * The path maps to the real file on disk. The file may not be valid. |
---|
| 429 | * |
---|
| 430 | * @return const std::string The real path to the file on disk. |
---|
| 431 | */ |
---|
| 432 | const std::string path () const { |
---|
| 433 | return name ().path (); |
---|
| 434 | } |
---|
| 435 | |
---|
| 436 | /** |
---|
[977c3de] | 437 | * Is the image open ? |
---|
[ec24a37] | 438 | * |
---|
[977c3de] | 439 | * @retval true The image is open. |
---|
| 440 | * @retval false The image is not open. |
---|
[ec24a37] | 441 | */ |
---|
| 442 | bool is_open () const { |
---|
| 443 | return fd () != -1; |
---|
| 444 | } |
---|
| 445 | |
---|
[977c3de] | 446 | /** |
---|
| 447 | * Is the image writable ? |
---|
| 448 | * |
---|
[fd8a2c5] | 449 | * @retval true The image is writable. |
---|
| 450 | * @retval false The image is not writable. |
---|
[977c3de] | 451 | */ |
---|
[fd8a2c5] | 452 | bool is_writable () const { |
---|
| 453 | return writable; |
---|
[977c3de] | 454 | } |
---|
| 455 | |
---|
[ec24a37] | 456 | private: |
---|
[977c3de] | 457 | |
---|
[ec24a37] | 458 | file name_; //< The name of the file. |
---|
| 459 | int references_; //< The number of handles open. |
---|
| 460 | int fd_; //< The file descriptor of the archive. |
---|
[977c3de] | 461 | elf::file elf_; //< The libelf reference. |
---|
[ec24a37] | 462 | int symbol_refs; //< The number of symbols references made. |
---|
[fd8a2c5] | 463 | bool writable; //< The image is writable. |
---|
[ec24a37] | 464 | }; |
---|
| 465 | |
---|
| 466 | /** |
---|
| 467 | * Copy the input section of the image to the output section. The file |
---|
| 468 | * positions in the images must be set before making the call. |
---|
[2bcaf0c] | 469 | * |
---|
| 470 | * @param in The input image. |
---|
| 471 | * @param out The output image. |
---|
| 472 | * @param size The amouint of data to copy. |
---|
[ec24a37] | 473 | */ |
---|
| 474 | void copy (image& in, image& out, size_t size); |
---|
| 475 | |
---|
| 476 | /** |
---|
[2bcaf0c] | 477 | * The archive class proivdes access to object files that are held in a AR |
---|
| 478 | * format file. GNU AR extensions are supported. The archive is a kind of |
---|
| 479 | * @ref image and provides the container for the @ref object's that it |
---|
| 480 | * contains. |
---|
[ec24a37] | 481 | */ |
---|
| 482 | class archive: |
---|
| 483 | public image |
---|
| 484 | { |
---|
| 485 | public: |
---|
| 486 | /** |
---|
| 487 | * Open a archive format file that contains ELF object files. |
---|
| 488 | * |
---|
[2bcaf0c] | 489 | * @param name The name of the @ref archive. |
---|
[ec24a37] | 490 | */ |
---|
| 491 | archive (const std::string& name); |
---|
| 492 | |
---|
| 493 | /** |
---|
| 494 | * Close the archive. |
---|
| 495 | */ |
---|
| 496 | virtual ~archive (); |
---|
| 497 | |
---|
[977c3de] | 498 | /** |
---|
| 499 | * Begin the ELF session. |
---|
| 500 | */ |
---|
| 501 | void begin (); |
---|
| 502 | |
---|
| 503 | /** |
---|
| 504 | * End the ELF session. |
---|
| 505 | */ |
---|
| 506 | void end (); |
---|
| 507 | |
---|
[ec24a37] | 508 | /** |
---|
| 509 | * Match the archive name. |
---|
| 510 | * |
---|
| 511 | * @param name The name of the archive to check. |
---|
[2bcaf0c] | 512 | * @retval true The name matches. |
---|
| 513 | * @retval false The name does not match. |
---|
[ec24a37] | 514 | */ |
---|
| 515 | bool is (const std::string& name) const; |
---|
| 516 | |
---|
| 517 | /** |
---|
| 518 | * Check this is a valid archive. |
---|
[2bcaf0c] | 519 | * |
---|
| 520 | * @retval true It is a valid archive. |
---|
| 521 | * @retval false It is not a valid archive. |
---|
[ec24a37] | 522 | */ |
---|
| 523 | bool is_valid (); |
---|
| 524 | |
---|
| 525 | /** |
---|
[2bcaf0c] | 526 | * Load @ref object's from the @ref archive adding each to the provided |
---|
| 527 | * @ref objects container. |
---|
| 528 | * |
---|
| 529 | * @param objs The container the loaded object files are added too. |
---|
[ec24a37] | 530 | */ |
---|
| 531 | void load_objects (objects& objs); |
---|
[977c3de] | 532 | |
---|
[ec24a37] | 533 | /** |
---|
| 534 | * Get the name. |
---|
[2bcaf0c] | 535 | * |
---|
| 536 | * @return const std::string& Return a reference to the archive's name. |
---|
[ec24a37] | 537 | */ |
---|
| 538 | const std::string& get_name () const; |
---|
| 539 | |
---|
| 540 | /** |
---|
[2bcaf0c] | 541 | * Less than operator for the map container. It compares the name of the |
---|
| 542 | * the @ref archive. |
---|
| 543 | * |
---|
| 544 | * @param rhs The right hand side of the '<' operator. |
---|
| 545 | * @return true The right hand side is less than this archive. |
---|
| 546 | * @return false The right hand side is greater than or equal to this |
---|
| 547 | * archive. |
---|
[ec24a37] | 548 | */ |
---|
| 549 | bool operator< (const archive& rhs) const; |
---|
| 550 | |
---|
| 551 | /** |
---|
[2bcaf0c] | 552 | * Create a new archive containing the given set of objects. If |
---|
| 553 | * referening an existing archive it is overwritten. |
---|
| 554 | * |
---|
| 555 | * @param objects The list of objects to place in the archive. |
---|
[ec24a37] | 556 | */ |
---|
| 557 | void create (object_list& objects); |
---|
| 558 | |
---|
| 559 | private: |
---|
[977c3de] | 560 | |
---|
[ec24a37] | 561 | /** |
---|
[2bcaf0c] | 562 | * Read the archive header and check the magic number is valid. |
---|
| 563 | * |
---|
| 564 | * @param offset The offset in the file to read the header. |
---|
| 565 | * @param header Read the header into here. There must be enough space. |
---|
| 566 | * @retval true The header was read successfull and the magic number |
---|
| 567 | * matched. |
---|
| 568 | * @retval false The header could not be read from the @ref image. |
---|
[ec24a37] | 569 | */ |
---|
| 570 | bool read_header (off_t offset, uint8_t* header); |
---|
| 571 | |
---|
| 572 | /** |
---|
| 573 | * Add the object file from the archive to the object's container. |
---|
[2bcaf0c] | 574 | * |
---|
| 575 | * @param objs The container to add the object to. |
---|
| 576 | * @param name The name of the object file being added. |
---|
| 577 | * @param offset The offset in the @ref archive of the object file. |
---|
| 578 | * @param size The size of the object file. |
---|
[ec24a37] | 579 | */ |
---|
| 580 | void add_object (objects& objs, |
---|
| 581 | const char* name, |
---|
[977c3de] | 582 | off_t offset, |
---|
[ec24a37] | 583 | size_t size); |
---|
| 584 | |
---|
| 585 | /** |
---|
| 586 | * Write a file header into the archive. |
---|
[2bcaf0c] | 587 | * |
---|
| 588 | * @param name The name of the archive. |
---|
| 589 | * @param mtime The modified time of the archive. |
---|
| 590 | * @param uid The user id of the archive. |
---|
| 591 | * @param gid The group id of the archive. |
---|
| 592 | * @param mode The mode of the archive. |
---|
| 593 | * @param size The size of the archive. |
---|
[ec24a37] | 594 | */ |
---|
| 595 | void write_header (const std::string& name, |
---|
| 596 | uint32_t mtime, |
---|
| 597 | int uid, |
---|
| 598 | int gid, |
---|
| 599 | int mode, |
---|
| 600 | size_t size); |
---|
| 601 | |
---|
| 602 | /** |
---|
| 603 | * Cannot copy via a copy constructor. |
---|
| 604 | */ |
---|
| 605 | archive (const archive& orig); |
---|
| 606 | |
---|
| 607 | /** |
---|
[2bcaf0c] | 608 | * Cannot assign using the assignment operator. |
---|
[ec24a37] | 609 | */ |
---|
| 610 | archive& operator= (const archive& rhs); |
---|
| 611 | }; |
---|
| 612 | |
---|
[90d8d43] | 613 | /** |
---|
| 614 | * A relocation record. We extract what we want because the elf::section |
---|
| 615 | * class requires the image be left open as references are alive. We |
---|
| 616 | * extract and keep the data we need to create the image. |
---|
| 617 | */ |
---|
| 618 | struct relocation |
---|
| 619 | { |
---|
| 620 | const uint32_t offset; //< The section offset. |
---|
| 621 | const uint32_t type; //< The type of relocation record. |
---|
| 622 | const uint32_t info; //< The ELF info field. |
---|
| 623 | const int32_t addend; //< The constant addend. |
---|
[42f766f] | 624 | const std::string symname; //< The name of the symbol. |
---|
| 625 | const uint32_t symtype; //< The type of symbol. |
---|
| 626 | const int symsect; //< The symbol's section symbol. |
---|
| 627 | const uint32_t symvalue; //< The symbol's value. |
---|
[c14133e] | 628 | const uint32_t symbinding;//< The symbol's binding. |
---|
[90d8d43] | 629 | |
---|
| 630 | /** |
---|
| 631 | * Construct from an ELF relocation record. |
---|
| 632 | */ |
---|
| 633 | relocation (const elf::relocation& er); |
---|
| 634 | |
---|
| 635 | private: |
---|
| 636 | /** |
---|
| 637 | * The default constructor is not allowed due to all elements being |
---|
| 638 | * const. |
---|
| 639 | */ |
---|
| 640 | relocation (); |
---|
| 641 | }; |
---|
| 642 | |
---|
| 643 | /** |
---|
| 644 | * A container of relocations. |
---|
| 645 | */ |
---|
| 646 | typedef std::list < relocation > relocations; |
---|
| 647 | |
---|
[a5fcdd5] | 648 | /** |
---|
| 649 | * The sections attributes. We extract what we want because the |
---|
| 650 | * elf::section class requires the image be left open as references are |
---|
| 651 | * alive. We extract and keep the data we need to create the image. |
---|
| 652 | */ |
---|
| 653 | struct section |
---|
| 654 | { |
---|
| 655 | const std::string name; //< The name of the section. |
---|
[662c504] | 656 | const int index; //< The section's index in the object file. |
---|
[a5fcdd5] | 657 | const uint32_t type; //< The type of section. |
---|
| 658 | const size_t size; //< The size of the section. |
---|
| 659 | const uint32_t alignment; //< The alignment of the section. |
---|
| 660 | const uint32_t link; //< The ELF link field. |
---|
| 661 | const uint32_t info; //< The ELF info field. |
---|
| 662 | const uint32_t flags; //< The ELF flags. |
---|
| 663 | const off_t offset; //< The ELF file offset. |
---|
[90d8d43] | 664 | bool rela; //< Relocation records have the addend field. |
---|
| 665 | relocations relocs; //< The sections relocations. |
---|
[a5fcdd5] | 666 | |
---|
| 667 | /** |
---|
| 668 | * Construct from an ELF section. |
---|
[90d8d43] | 669 | * |
---|
| 670 | * @param es The ELF section to load the object file section from. |
---|
[a5fcdd5] | 671 | */ |
---|
| 672 | section (const elf::section& es); |
---|
| 673 | |
---|
[90d8d43] | 674 | /** |
---|
| 675 | * Load the ELF relocations. |
---|
| 676 | * |
---|
| 677 | * @param es The ELF section to load the relocations from. |
---|
| 678 | */ |
---|
| 679 | void load_relocations (const elf::section& es); |
---|
| 680 | |
---|
[a5fcdd5] | 681 | private: |
---|
| 682 | /** |
---|
| 683 | * The default constructor is not allowed due to all elements being |
---|
| 684 | * const. |
---|
| 685 | */ |
---|
| 686 | section (); |
---|
| 687 | }; |
---|
| 688 | |
---|
| 689 | /** |
---|
| 690 | * A container of sections. |
---|
| 691 | */ |
---|
| 692 | typedef std::list < section > sections; |
---|
| 693 | |
---|
| 694 | /** |
---|
| 695 | * Sum the sizes of a container of sections. |
---|
| 696 | */ |
---|
| 697 | size_t sum_sizes (const sections& secs); |
---|
| 698 | |
---|
[662c504] | 699 | /** |
---|
| 700 | * Find the section that matches the index in the sections provided. |
---|
| 701 | */ |
---|
| 702 | const section* find (const sections& secs, const int index); |
---|
| 703 | |
---|
[ec24a37] | 704 | /** |
---|
| 705 | * The object file cab be in an archive or a file. |
---|
| 706 | */ |
---|
| 707 | class object: |
---|
| 708 | public image |
---|
| 709 | { |
---|
| 710 | public: |
---|
| 711 | /** |
---|
| 712 | * Construct an object image that is part of an archive. |
---|
| 713 | * |
---|
| 714 | * @param archive_ The archive the object file is part of. |
---|
| 715 | * @param file_ The image file. |
---|
| 716 | */ |
---|
| 717 | object (archive& archive_, file& file_); |
---|
| 718 | |
---|
| 719 | /** |
---|
| 720 | * Construct the object file. |
---|
| 721 | * |
---|
| 722 | * @param path The object file path. |
---|
| 723 | */ |
---|
| 724 | object (const std::string& path); |
---|
| 725 | |
---|
| 726 | /** |
---|
| 727 | * Construct the object file. |
---|
| 728 | */ |
---|
| 729 | object (); |
---|
| 730 | |
---|
| 731 | /** |
---|
| 732 | * Destruct the object file. |
---|
| 733 | */ |
---|
| 734 | virtual ~object (); |
---|
| 735 | |
---|
| 736 | /** |
---|
| 737 | * Open the object file. |
---|
| 738 | */ |
---|
[596e5fa] | 739 | virtual void open (bool writable = false); |
---|
[ec24a37] | 740 | |
---|
| 741 | /** |
---|
| 742 | * Close the object. |
---|
| 743 | */ |
---|
| 744 | virtual void close (); |
---|
| 745 | |
---|
| 746 | /** |
---|
[a5fcdd5] | 747 | * Begin the object file session. |
---|
[ec24a37] | 748 | */ |
---|
| 749 | void begin (); |
---|
| 750 | |
---|
| 751 | /** |
---|
[a5fcdd5] | 752 | * End the object file session. |
---|
[ec24a37] | 753 | */ |
---|
| 754 | void end (); |
---|
| 755 | |
---|
[a5fcdd5] | 756 | /** |
---|
| 757 | * If valid returns true the begin has been called and the object has |
---|
| 758 | * been validated as being in a suitable format. |
---|
| 759 | */ |
---|
| 760 | bool valid () const; |
---|
| 761 | |
---|
[ec24a37] | 762 | /** |
---|
| 763 | * Load the symbols into the symbols table. |
---|
| 764 | * |
---|
| 765 | * @param symbols The symbol table to load. |
---|
| 766 | * @param local Include local symbols. The default is not to. |
---|
| 767 | */ |
---|
[a5fcdd5] | 768 | void load_symbols (symbols::table& symbols, bool local = false); |
---|
[ec24a37] | 769 | |
---|
[90d8d43] | 770 | /** |
---|
| 771 | * Load the relocations. |
---|
| 772 | */ |
---|
| 773 | void load_relocations (); |
---|
| 774 | |
---|
[ec24a37] | 775 | /** |
---|
| 776 | * References to the image. |
---|
| 777 | */ |
---|
| 778 | virtual int references () const; |
---|
| 779 | |
---|
| 780 | /** |
---|
| 781 | * The file size. |
---|
| 782 | */ |
---|
| 783 | virtual size_t size () const; |
---|
| 784 | |
---|
| 785 | /** |
---|
| 786 | * The file descriptor. |
---|
| 787 | */ |
---|
| 788 | virtual int fd () const; |
---|
| 789 | |
---|
| 790 | /** |
---|
| 791 | * A symbol in the image has been referenced. |
---|
| 792 | */ |
---|
| 793 | virtual void symbol_referenced (); |
---|
| 794 | |
---|
| 795 | /** |
---|
| 796 | * The archive the object file is contained in. If 0 the object file is |
---|
| 797 | * not contained in an archive. |
---|
| 798 | */ |
---|
| 799 | archive* get_archive (); |
---|
| 800 | |
---|
| 801 | /** |
---|
| 802 | * Return the unresolved symbol table for this object file. |
---|
| 803 | */ |
---|
[544de91] | 804 | symbols::symtab& unresolved_symbols (); |
---|
[ec24a37] | 805 | |
---|
| 806 | /** |
---|
| 807 | * Return the list external symbols. |
---|
| 808 | */ |
---|
[a5fcdd5] | 809 | symbols::pointers& external_symbols (); |
---|
| 810 | |
---|
| 811 | /** |
---|
| 812 | * Return a container sections that match the requested type and |
---|
| 813 | * flags. The filtered section container is not cleared so any matching |
---|
| 814 | * sections are appended. |
---|
| 815 | * |
---|
[e78e2b0] | 816 | * @param filtered_secs The container of the matching sections. |
---|
[a5fcdd5] | 817 | * @param type The section type. Must match. If 0 matches any. |
---|
| 818 | * @param flags_in The sections flags that must be set. This is a |
---|
| 819 | * mask. If 0 matches any. |
---|
| 820 | * @param flags_out The sections flags that must be clear. This is a |
---|
| 821 | * mask. If 0 this value is ignored. |
---|
| 822 | */ |
---|
| 823 | void get_sections (sections& filtered_secs, |
---|
| 824 | uint32_t type = 0, |
---|
| 825 | uint64_t flags_in = 0, |
---|
| 826 | uint64_t flags_out = 0); |
---|
| 827 | |
---|
| 828 | /** |
---|
| 829 | * Return a container sections that match the requested name. The |
---|
| 830 | * filtered section container is not cleared so any matching sections are |
---|
| 831 | * appended. |
---|
| 832 | * |
---|
[e78e2b0] | 833 | * @param filtered_secs The container of the matching sections. |
---|
[a5fcdd5] | 834 | * @param name The name of the section. |
---|
| 835 | */ |
---|
| 836 | void get_sections (sections& filtered_secs, const std::string& name); |
---|
[ec24a37] | 837 | |
---|
[53221a0] | 838 | /** |
---|
| 839 | * Get a section given an index number. |
---|
| 840 | * |
---|
| 841 | * @param index The section index to search for. |
---|
| 842 | */ |
---|
| 843 | const section& get_section (int index) const; |
---|
| 844 | |
---|
[544de91] | 845 | /** |
---|
| 846 | * Set the object file's resolving flag. |
---|
| 847 | */ |
---|
| 848 | void resolve_set (); |
---|
| 849 | |
---|
| 850 | /** |
---|
| 851 | * Clear the object file's resolving flag. |
---|
| 852 | */ |
---|
| 853 | void resolve_clear (); |
---|
| 854 | |
---|
| 855 | /** |
---|
| 856 | * The resolving state. |
---|
| 857 | */ |
---|
| 858 | bool resolving () const; |
---|
| 859 | |
---|
| 860 | /** |
---|
| 861 | * Set the object file resolved flag. |
---|
| 862 | */ |
---|
| 863 | void resolved_set (); |
---|
| 864 | |
---|
| 865 | /** |
---|
| 866 | * The resolved state. |
---|
| 867 | */ |
---|
| 868 | bool resolved () const; |
---|
| 869 | |
---|
[ec24a37] | 870 | private: |
---|
[a5fcdd5] | 871 | archive* archive_; //< Points to the archive if part of an |
---|
| 872 | // archive. |
---|
| 873 | bool valid_; //< If true begin has run and finished. |
---|
[544de91] | 874 | symbols::symtab unresolved; //< This object's unresolved symbols. |
---|
[a5fcdd5] | 875 | symbols::pointers externals; //< This object's external symbols. |
---|
| 876 | sections secs; //< The sections. |
---|
[544de91] | 877 | bool resolving_; //< The object is being resolved. |
---|
| 878 | bool resolved_; //< The object has been resolved. |
---|
[ec24a37] | 879 | |
---|
| 880 | /** |
---|
| 881 | * Cannot copy via a copy constructor. |
---|
| 882 | */ |
---|
| 883 | object (const object& orig); |
---|
| 884 | |
---|
| 885 | /** |
---|
| 886 | * Cannot assign using the assignment operator. |
---|
| 887 | */ |
---|
| 888 | object& operator= (const object& rhs); |
---|
| 889 | }; |
---|
| 890 | |
---|
| 891 | /** |
---|
| 892 | * A collection of objects files as a cache. This currently is not a cache |
---|
| 893 | * but it could become one. |
---|
| 894 | */ |
---|
| 895 | class cache |
---|
| 896 | { |
---|
| 897 | public: |
---|
| 898 | /** |
---|
| 899 | * Construct the cache. |
---|
| 900 | */ |
---|
| 901 | cache (); |
---|
| 902 | |
---|
| 903 | /** |
---|
| 904 | * Destruct the objects. |
---|
| 905 | */ |
---|
| 906 | virtual ~cache (); |
---|
| 907 | |
---|
| 908 | /** |
---|
| 909 | * Open the cache by collecting the file names, loading object headers |
---|
| 910 | * and loading the archive file names. |
---|
| 911 | */ |
---|
| 912 | void open (); |
---|
| 913 | |
---|
| 914 | /** |
---|
| 915 | * Close the cache. |
---|
| 916 | */ |
---|
| 917 | void close (); |
---|
| 918 | |
---|
| 919 | /** |
---|
| 920 | * Add a file path to the cache. |
---|
| 921 | */ |
---|
| 922 | void add (const std::string& path); |
---|
| 923 | |
---|
| 924 | /** |
---|
| 925 | * Add a container of path to the cache. |
---|
| 926 | */ |
---|
| 927 | void add (paths& paths__); |
---|
| 928 | |
---|
| 929 | /** |
---|
| 930 | * Add a container of path to the cache. |
---|
| 931 | */ |
---|
| 932 | void add_libraries (paths& paths__); |
---|
| 933 | |
---|
| 934 | /** |
---|
[977c3de] | 935 | * Being a session on an archive. |
---|
[ec24a37] | 936 | */ |
---|
| 937 | void archive_begin (const std::string& path); |
---|
| 938 | |
---|
| 939 | /** |
---|
[977c3de] | 940 | * End a session on an archive. |
---|
[ec24a37] | 941 | */ |
---|
| 942 | void archive_end (const std::string& path); |
---|
| 943 | |
---|
| 944 | /** |
---|
[977c3de] | 945 | * Being sessions on all archives. |
---|
[ec24a37] | 946 | */ |
---|
| 947 | void archives_begin (); |
---|
| 948 | |
---|
| 949 | /** |
---|
[977c3de] | 950 | * End the archive sessions. |
---|
[ec24a37] | 951 | */ |
---|
| 952 | void archives_end (); |
---|
| 953 | |
---|
| 954 | /** |
---|
| 955 | * Collect the object names and add them to the cache. |
---|
| 956 | */ |
---|
| 957 | void collect_object_files (); |
---|
[977c3de] | 958 | |
---|
[ec24a37] | 959 | /** |
---|
| 960 | * Collect the object file names by verifing the paths to the files are |
---|
| 961 | * valid or read the object file names contained in any archives. |
---|
| 962 | */ |
---|
| 963 | void collect_object_files (const std::string& path); |
---|
[977c3de] | 964 | |
---|
[ec24a37] | 965 | /** |
---|
| 966 | * Load the symbols into the symbol table. |
---|
| 967 | * |
---|
| 968 | * @param symbols The symbol table to load. |
---|
[e78e2b0] | 969 | * @param locals Include local symbols. The default does not include them. |
---|
[ec24a37] | 970 | */ |
---|
[a5fcdd5] | 971 | void load_symbols (symbols::table& symbols, bool locals = false); |
---|
[ec24a37] | 972 | |
---|
| 973 | /** |
---|
| 974 | * Output the unresolved symbol table to the output stream. |
---|
| 975 | */ |
---|
| 976 | void output_unresolved_symbols (std::ostream& out); |
---|
| 977 | |
---|
| 978 | /** |
---|
| 979 | * Get the archives. |
---|
| 980 | */ |
---|
| 981 | archives& get_archives (); |
---|
[977c3de] | 982 | |
---|
[ec24a37] | 983 | /** |
---|
| 984 | * Get the objects inlcuding those in archives. |
---|
| 985 | */ |
---|
| 986 | objects& get_objects (); |
---|
[977c3de] | 987 | |
---|
[ec24a37] | 988 | /** |
---|
| 989 | * Get the added objects. Does not include the ones in th archives. |
---|
| 990 | */ |
---|
[b770b0c] | 991 | void get_objects (object_list& list) const; |
---|
[ec24a37] | 992 | |
---|
| 993 | /** |
---|
| 994 | * Get the paths. |
---|
| 995 | */ |
---|
| 996 | const paths& get_paths () const; |
---|
| 997 | |
---|
| 998 | /** |
---|
| 999 | * Get the archive files. |
---|
| 1000 | */ |
---|
| 1001 | void get_archive_files (files& afiles); |
---|
| 1002 | |
---|
| 1003 | /** |
---|
| 1004 | * Get the object files including those in archives. |
---|
| 1005 | */ |
---|
| 1006 | void get_object_files (files& ofiles); |
---|
| 1007 | |
---|
| 1008 | /** |
---|
| 1009 | * Get the archive count. |
---|
| 1010 | */ |
---|
| 1011 | int archive_count () const; |
---|
| 1012 | |
---|
| 1013 | /** |
---|
| 1014 | * Get the object count. |
---|
| 1015 | */ |
---|
| 1016 | int object_count () const; |
---|
| 1017 | |
---|
| 1018 | /** |
---|
| 1019 | * Get the path count. |
---|
| 1020 | */ |
---|
| 1021 | int path_count () const; |
---|
| 1022 | |
---|
| 1023 | /** |
---|
| 1024 | * Output archive files. |
---|
| 1025 | */ |
---|
| 1026 | void output_archive_files (std::ostream& out); |
---|
| 1027 | |
---|
| 1028 | /** |
---|
| 1029 | * Output archive files. |
---|
| 1030 | */ |
---|
| 1031 | void output_object_files (std::ostream& out); |
---|
| 1032 | |
---|
| 1033 | protected: |
---|
| 1034 | |
---|
| 1035 | /** |
---|
| 1036 | * Input a path into the cache. |
---|
| 1037 | */ |
---|
| 1038 | virtual void input (const std::string& path); |
---|
| 1039 | |
---|
| 1040 | private: |
---|
| 1041 | paths paths_; //< The names of the files to process. |
---|
| 1042 | archives archives_; //< The archive files. |
---|
| 1043 | objects objects_; //< The object files. |
---|
| 1044 | bool opened; //< The cache is open. |
---|
| 1045 | }; |
---|
| 1046 | |
---|
[fe19d06] | 1047 | /** |
---|
| 1048 | * Copy the in file to the out file. |
---|
| 1049 | * |
---|
| 1050 | * @param in The input file. |
---|
| 1051 | * @param out The output file. |
---|
| 1052 | * @param size The amount to copy. If 0 the whole on in is copied. |
---|
| 1053 | */ |
---|
| 1054 | void copy_file (image& in, image& out, size_t size = 0); |
---|
| 1055 | |
---|
[ec24a37] | 1056 | /** |
---|
| 1057 | * Find the libraries given the list of libraries as bare name which |
---|
| 1058 | * have 'lib' and '.a' added. |
---|
| 1059 | */ |
---|
| 1060 | void find_libraries (paths& libraries, paths& libpaths, paths& libs); |
---|
| 1061 | |
---|
| 1062 | } |
---|
| 1063 | } |
---|
| 1064 | |
---|
| 1065 | #endif |
---|