| 1 | // object.h -- support for an object file for linking in gold -*- C++ -*- |
| 2 | |
| 3 | #ifndef GOLD_OBJECT_H |
| 4 | #define GOLD_OBJECT_H |
| 5 | |
| 6 | #include "elfcpp.h" |
| 7 | #include "targetsize.h" |
| 8 | #include "target.h" |
| 9 | #include "fileread.h" |
| 10 | |
| 11 | namespace gold |
| 12 | { |
| 13 | |
| 14 | // Data to pass from read_symbols() to add_symbols(). |
| 15 | |
| 16 | struct Read_symbols_data |
| 17 | { |
| 18 | // Symbol data. |
| 19 | File_view* symbols; |
| 20 | // Size of symbol data in bytes. |
| 21 | off_t symbols_size; |
| 22 | // Symbol names. |
| 23 | File_view* symbol_names; |
| 24 | // Size of symbol name data in bytes. |
| 25 | off_t symbol_names_size; |
| 26 | }; |
| 27 | |
| 28 | // Object is an interface which represents either a 32-bit or a 64-bit |
| 29 | // object file. The actual instantiations are Sized_object<32> and |
| 30 | // Sized_object<64> |
| 31 | |
| 32 | class Object |
| 33 | { |
| 34 | public: |
| 35 | // NAME is the name of the object as we would report it to the user |
| 36 | // (e.g., libfoo.a(bar.o) if this is in an archive. INPUT_FILE is |
| 37 | // used to read the file. OFFSET is the offset within the input |
| 38 | // file--0 for a .o or .so file, something else for a .a file. |
| 39 | Object(const std::string& name, Input_file* input_file, off_t offset = 0) |
| 40 | : name_(name), input_file_(input_file), offset_(offset) |
| 41 | { } |
| 42 | |
| 43 | virtual ~Object() |
| 44 | { } |
| 45 | |
| 46 | const std::string& |
| 47 | name() const |
| 48 | { return this->name_; } |
| 49 | |
| 50 | // Read the symbol and relocation information. |
| 51 | Read_symbols_data |
| 52 | read_symbols() |
| 53 | { return this->do_read_symbols(); } |
| 54 | |
| 55 | // Add symbol information to the global symbol table. |
| 56 | void |
| 57 | add_symbols(Read_symbols_data rd) |
| 58 | { this->do_add_symbols(rd); } |
| 59 | |
| 60 | protected: |
| 61 | // Read the symbols--implemented by child class. |
| 62 | virtual Read_symbols_data |
| 63 | do_read_symbols() = 0; |
| 64 | |
| 65 | // Add symbol information to the global symbol table--implemented by |
| 66 | // child class. |
| 67 | virtual void |
| 68 | do_add_symbols(Read_symbols_data) = 0; |
| 69 | |
| 70 | // Get the file. |
| 71 | Input_file* |
| 72 | input_file() const |
| 73 | { return this->input_file_; } |
| 74 | |
| 75 | // Get the offset into the file. |
| 76 | off_t |
| 77 | offset() const |
| 78 | { return this->offset_; } |
| 79 | |
| 80 | // Get a view into the underlying file. |
| 81 | const unsigned char* |
| 82 | get_view(off_t start, off_t size); |
| 83 | |
| 84 | // Read data from the underlying file. |
| 85 | void |
| 86 | read(off_t start, off_t size, void* p); |
| 87 | |
| 88 | // Get a lasting view into the underlying file. |
| 89 | File_view* |
| 90 | get_lasting_view(off_t start, off_t size); |
| 91 | |
| 92 | private: |
| 93 | // This class may not be copied. |
| 94 | Object(const Object&); |
| 95 | Object& operator=(const Object&); |
| 96 | |
| 97 | // Name of object as printed to use. |
| 98 | std::string name_; |
| 99 | // For reading the file. |
| 100 | Input_file* input_file_; |
| 101 | // Offset within the file--0 for an object file, non-0 for an |
| 102 | // archive. |
| 103 | off_t offset_; |
| 104 | }; |
| 105 | |
| 106 | // The functions of Object which are size specific. |
| 107 | |
| 108 | template<int size, bool big_endian> |
| 109 | class Sized_object : public Object |
| 110 | { |
| 111 | public: |
| 112 | Sized_object(const std::string& name, Input_file* input_file, off_t offset, |
| 113 | const typename elfcpp::Ehdr<size, big_endian>&); |
| 114 | |
| 115 | ~Sized_object(); |
| 116 | |
| 117 | void |
| 118 | setup(const typename elfcpp::Ehdr<size, big_endian>&); |
| 119 | |
| 120 | Read_symbols_data |
| 121 | do_read_symbols(); |
| 122 | |
| 123 | void |
| 124 | do_add_symbols(Read_symbols_data); |
| 125 | |
| 126 | private: |
| 127 | // This object may not be copied. |
| 128 | Sized_object(const Sized_object&); |
| 129 | Sized_object& operator=(const Sized_object&); |
| 130 | |
| 131 | // ELF file header EI_OSABI field. |
| 132 | unsigned char osabi_; |
| 133 | // ELF file header EI_ABIVERSION field. |
| 134 | unsigned char abiversion_; |
| 135 | // ELF file header e_machine field. |
| 136 | elfcpp::Elf_Half machine_; |
| 137 | // ELF file header e_flags field. |
| 138 | unsigned int flags_; |
| 139 | // Target functions--may be NULL. |
| 140 | Target* target_; |
| 141 | // File offset of section header table. |
| 142 | off_t shoff_; |
| 143 | // Number of input sections. |
| 144 | unsigned int shnum_; |
| 145 | // Offset of SHT_STRTAB section holding section names. |
| 146 | unsigned int shstrndx_; |
| 147 | // Index of SHT_SYMTAB section. |
| 148 | unsigned int symtab_shnum_; |
| 149 | }; |
| 150 | |
| 151 | // Return an Object appropriate for the input file. P is BYTES long, |
| 152 | // and holds the ELF header. |
| 153 | |
| 154 | extern Object* make_elf_object(const std::string& name, Input_file*, |
| 155 | off_t offset, const unsigned char* p, |
| 156 | off_t bytes); |
| 157 | |
| 158 | } // end namespace gold |
| 159 | |
| 160 | #endif // !defined(GOLD_OBJECT_H) |