source: rtems-tools/linkers/rld-elf.h @ be8e188

4.104.115
Last change on this file since be8e188 was 4c89c2d, checked in by Chris Johns <chrisj@…>, on 11/25/12 at 23:59:19

Update the section details.

  • Property mode set to 100644
File size: 19.3 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 ELF module manages the libelf interface.
22 *
23 */
24
25#if !defined (_RLD_ELF_H_)
26#define _RLD_ELF_H_
27
28#include <list>
29#include <map>
30
31#include <rld.h>
32
33namespace rld
34{
35  namespace elf
36  {
37    /**
38     * Forward decl.
39     */
40    class file;
41
42    /**
43     * An ELF Section. The current implementation only supports a single data
44     * descriptor with a section.
45     */
46    class section
47    {
48    public:
49      /**
50       * Construct the section getting the details from the ELF file given the
51       * section index.
52       *
53       * The section types are (from elf(3)):
54       *
55       *  Section Type         Library Type     Description
56       *  ------------         ------------     -----------
57       *   SHT_DYNAMIC          ELF_T_DYN        `.dynamic' section entries.
58       *   SHT_DYNSYM           ELF_T_SYM        Symbols for dynamic linking.
59       *   SHT_FINI_ARRAY       ELF_T_ADDR       Termination function pointers.
60       *   SHT_GROUP            ELF_T_WORD       Section group marker.
61       *   SHT_HASH             ELF_T_HASH       Symbol hashes.
62       *   SHT_INIT_ARRAY       ELF_T_ADDR       Initialization function pointers.
63       *   SHT_NOBITS           ELF_T_BYTE       Empty sections.  See elf(5).
64       *   SHT_NOTE             ELF_T_NOTE       ELF note records.
65       *   SHT_PREINIT_ARRAY    ELF_T_ADDR       Pre-initialization function
66       *                                         pointers.
67       *   SHT_PROGBITS         ELF_T_BYTE       Machine code.
68       *   SHT_REL              ELF_T_REL        ELF relocation records.
69       *   SHT_RELA             ELF_T_RELA       Relocation records with addends.
70       *   SHT_STRTAB           ELF_T_BYTE       String tables.
71       *   SHT_SYMTAB           ELF_T_SYM        Symbol tables.
72       *   SHT_SYMTAB_SHNDX     ELF_T_WORD       Used with extended section
73       *                                         numbering.
74       *   SHT_GNU_verdef       ELF_T_VDEF       Symbol version definitions.
75       *   SHT_GNU_verneed      ELF_T_VNEED      Symbol versioning requirements.
76       *   SHT_GNU_versym       ELF_T_HALF       Version symbols.
77       *   SHT_SUNW_move        ELF_T_MOVE       ELF move records.
78       *   SHT_SUNW_syminfo     ELF_T_SYMINFO    Additional symbol flags.
79       *
80       * @param file_ The ELF file this section is part of.
81       * @param index_ The section's index.
82       * @param name The section's name.
83       * @param type The section's type.
84       * @param alignment The section's alignment.
85       * @param flags The section's flags.
86       * @param addr The section's in-memory address.
87       * @param offset The section's offset in the file.
88       * @param size The section's file in bytes.
89       * @param link The section's header table link.
90       * @param info The section's extra information.
91       * @param entry_size The section's entry size.
92       */
93      section (file&              file_,
94               int                index_,
95               const std::string& name,
96               elf_word           type,
97               elf_xword          alignment,
98               elf_xword          flags,
99               elf_addr           addr,
100               elf_off            offset,
101               elf_xword          size,
102               elf_word           link = 0,
103               elf_word           info = 0,
104               elf_xword          entry_size = 0);
105
106      /**
107       * Construct the section given the details. The ELF file must be
108       * writable.
109       *
110       * @param file_ The ELF file this section is part of.
111       * @param index The section's index in the ELF file.
112       */
113      section (file& file_, int index);
114
115      /**
116       * Copy constructor.
117       */
118      section (const section& orig);
119
120      /**
121       * Default constructor.
122       */
123      section ();
124
125      /**
126       * Add a data segment descriptor to the section if the file is writable.
127       *
128       * These are following data types (from elf(3)):
129       *
130       *   ELF_T_ADDR     Machine addresses.
131       *   ELF_T_BYTE     Byte data.  The library will not attempt to translate
132       *                  byte data.
133       *   ELF_T_CAP      Software and hardware capability records.
134       *   ELF_T_DYN      Records used in a section of type SHT_DYNAMIC.
135       *   ELF_T_EHDR     ELF executable header.
136       *   ELF_T_HALF     16-bit unsigned words.
137       *   ELF_T_LWORD    64 bit unsigned words.
138       *   ELF_T_MOVE     ELF Move records.
139       *   ELF_T_NOTE     ELF Note structures.
140       *   ELF_T_OFF      File offsets.
141       *   ELF_T_PHDR     ELF program header table entries.
142       *   ELF_T_REL      ELF relocation entries.
143       *   ELF_T_RELA     ELF relocation entries with addends.
144       *   ELF_T_SHDR     ELF section header entries.
145       *   ELF_T_SWORD    Signed 32-bit words.
146       *   ELF_T_SXWORD   Signed 64-bit words.
147       *   ELF_T_SYMINFO  ELF symbol information.
148       *   ELF_T_SYM      ELF symbol table entries.
149       *   ELF_T_VDEF     Symbol version definition records.
150       *   ELF_T_VNEED    Symbol version requirement records.
151       *   ELF_T_WORD     Unsigned 32-bit words.
152       *   ELF_T_XWORD    Unsigned 64-bit words.
153       *
154       * @param type The type of data in the segment.
155       * @param alignment The in-file alignment of the data. Must be a power of 2.
156       * @param size The number of bytes in this data descriptor.
157       * @param buffer The data in memory.
158       * @param offset The offset within the containing section. Can be computed.
159       */
160      void add_data (elf_type  type,
161                     elf_xword alignment,
162                     elf_xword size,
163                     void*     buffer = 0,
164                     elf_off   offset = 0);
165
166       /**
167       * The section's index in the ELF file.
168       *
169       * @return int The section number.
170       */
171      int index () const;
172
173      /**
174       * The name of the section.
175       *
176       * @return const std::string& The section's name.
177       */
178      const std::string& name () const;
179
180      /**
181       * The section's data.
182       */
183      elf_data* data ();
184
185      /**
186       * Get the type of the section.
187       */
188      elf_word type () const;
189
190      /**
191       * The section flags.
192       */
193      elf_xword flags () const;
194
195      /**
196       * In-memory address of the section.
197       */
198      elf_addr address () const;
199
200      /**
201       * Alignment constraint.
202       */
203      elf_xword alignment () const;
204
205      /**
206       * The file offset of the section.
207       */
208      elf_off offset () const;
209
210      /**
211       * The header table link.
212       */
213      elf_word link () const;
214
215      /**
216       * Extra information.
217       */
218      elf_word info () const;
219
220      /**
221       * Size of the section.
222       */
223      elf_xword size () const;
224
225      /**
226       * Size of the entries in the section.
227       */
228      elf_xword entry_size () const;
229
230      /**
231       * Number of entries.
232       */
233      int entries () const;
234
235      /**
236       * Set the name index if writable. This is normally done
237       * automatically when adding the section to the file.
238       */
239      void set_name (unsigned int index);
240
241    private:
242
243      /**
244       * Check the section is valid.
245       *
246       * @param where Where the check is being made.
247       */
248      void check (const char* where) const;
249
250      /**
251       * Check the section is valid and writable.
252       *
253       * @param where Where the check is being made.
254       */
255      void check_writable (const char* where) const;
256
257      file*       file_;  //< The ELF file.
258      int         index_; //< The section header index.
259      std::string name_;  //< The section's name.
260      elf_scn*    scn;    //< ELF private section data.
261      elf_shdr    shdr;   //< The section header.
262      elf_data*   data_;  //< The section's data.
263    };
264
265    /**
266     * Container of ELF section pointers.
267     */
268    typedef std::list < section* > sections;
269
270    /**
271     * Container of ELF section as a map, ie associative array.
272     */
273    typedef std::map < std::string, section > section_table;
274
275    /**
276     * An ELF program header.
277     */
278    class program_header
279    {
280    public:
281      /**
282       * Construct a program header.
283       */
284      program_header ();
285
286      /**
287       * Desctruct a program header.
288       */
289      ~program_header ();
290
291      /**
292       * Set the program header.
293       *
294       * @param type The type of segment.
295       * @param flags The segment's flags.
296       * @param offset The offet to segment.
297       * @param filesz The segment size in the file.
298       * @param memsz The segment size in memory.
299       * @param vaddr The virtual address in memory.
300       * @param paddr The physical address if any.
301       */
302      void set (elf_word type,
303                elf_word flags,
304                elf_off offset,
305                elf_xword filesz,
306                elf_xword memsz,
307                elf_xword align,
308                elf_addr vaddr,
309                elf_addr paddr = 0);
310
311    private:
312
313      elf_phdr phdr;  //< The ELF program header.
314    };
315
316    /**
317     * A container of program headers.
318     */
319    typedef std::list < program_header > program_headers;
320
321     /**
322     * An ELF file.
323     */
324    class file
325    {
326    public:
327     /**
328       * Construct an ELF file.
329       */
330      file ();
331
332      /**
333       * Destruct the ELF file object.
334       */
335      ~file ();
336
337      /**
338       * Begin using the ELF file.
339       *
340       * @param name The full name of the file.
341       * @param fd The file descriptor to read or write the file.
342       * @param writable The file is writeable. The default is false.
343       */
344      void begin (const std::string& name, int fd, const bool writable = false);
345
346      /**
347       * Begin using the ELF file in an archive.
348       *
349       * @param name The full name of the file.
350       * @param archive The file that is the archive.
351       * @param offset The offset of the ELF file in the archive.
352       */
353      void begin (const std::string& name, file& archive, off_t offset);
354
355      /**
356       * End using the ELF file.
357       */
358      void end ();
359
360      /**
361       * Write the ELF file creating it if it is writable. You should have
362       * added the sections and the data segment descriptors to the sections
363       * before calling write.
364       */
365      void write ();
366
367      /**
368       * Load the header. Done automatically.
369       */
370      void load_header ();
371
372      /**
373       * Get the machine type.
374       */
375      unsigned int machinetype () const;
376
377      /**
378       * Get the type of ELF file.
379       */
380      unsigned int type () const;
381
382      /**
383       * Get the class of the object file.
384       */
385      unsigned int object_class () const;
386
387      /**
388       * Get the data type, ie LSB or MSB.
389       */
390      unsigned int data_type () const;
391
392      /**
393       * Is the file an archive format file ?
394       */
395      bool is_archive () const;
396
397      /**
398       * Is the file an executable ?
399       */
400      bool is_executable () const;
401
402      /**
403       * Is the file relocatable ?
404       */
405      bool is_relocatable() const;
406
407      /**
408       * The number of sections in the file.
409       */
410      int section_count () const;
411
412      /**
413       * Load the sections.
414       */
415      void load_sections ();
416
417      /**
418       * Get a filtered container of the sections. The key is the section
419       * type. If the sections are not loaded they are loaded. If the type is 0
420       * all sections are returned.
421       *
422       * @param filtered_secs The container the copy of the filtered sections
423       *                      are placed in.
424       * @param type The type of sections to filter on. If 0 all sections are
425       *             matched.
426       */
427      void get_sections (sections& filtered_secs, unsigned int type);
428
429      /**
430       * Return the index of the string section.
431       */
432      int strings_section () const;
433
434      /**
435       * Get the string from the specified section at the requested offset.
436       *
437       * @param section The section to search for the string.
438       * @param offset The offset in the string section.
439       * @return std::string The string.
440       */
441      std::string get_string (int section, size_t offset);
442
443      /**
444       * Get the string from the ELF header declared string section at the
445       * requested offset.
446       *
447       * @param offset The offset in the string section.
448       * @return std::string The string.
449       */
450      std::string get_string (size_t offset);
451
452      /**
453       * Load the symbols.
454       */
455      void load_symbols ();
456
457      /**
458       * Get a filtered container of symbols given the various types. If the
459       * symbols are not loaded they are loaded.
460       *
461       * @param filter_syms The filtered symbols found in the file. This is a
462       *                    container of pointers.
463       * @param local Return local symbols.
464       * @param weak Return weak symbols.
465       * @param global Return global symbols.
466       * @param unresolved Return unresolved symbols.
467       */
468      void get_symbols (rld::symbols::pointers& filtered_syms,
469                        bool                    unresolved = false,
470                        bool                    local = false,
471                        bool                    weak = true,
472                        bool                    global = true);
473
474      /**
475       * Set the ELF header. Must be writable.
476       *
477       * The classes are:
478       *   ELFCLASSNONE  This class is invalid.
479       *   ELFCLASS32    This defines the 32-bit architecture.  It sup- ports
480       *                 machines with files and virtual address spa- ces up to
481       *                 4 Gigabytes.
482       *   ELFCLASS64    This defines the 64-bit architecture.
483       *
484       * The types are:
485       *   ET_NONE  An unknown type.
486       *   ET_REL   A relocatable file.
487       *   ET_EXEC  An executable file.
488       *   ET_DYN   A shared object.
489       *   ET_CORE  A core file.
490       *
491       * The machine types are:
492       *   TDB
493       *
494       * The datatypes are:
495       *   ELFDATA2LSB  Two's complement, little-endian.
496       *   ELFDATA2MSB  Two's complement, big-endian.
497       *
498       * @param type The type of ELF file, ie executable, relocatable etc.
499       * @param class_ The files ELF class.
500       * @param machinetype The type of machine code present in the ELF file.
501       * @param datatype The data type, ie LSB or MSB.
502       */
503      void set_header (elf_half      type,
504                       int           class_,
505                       elf_half      machinetype,
506                       unsigned char datatype);
507
508      /**
509       * Add a section to the ELF file if writable.
510       */
511      void add (section& sec);
512
513      /**
514       * Add a program header to the ELF file if writable.
515       */
516      void add (program_header& phdr);
517
518      /**
519       * Get the ELF reference.
520       */
521      elf* get_elf ();
522
523      /**
524       * Get the name of the file.
525       */
526      const std::string& name () const;
527
528      /**
529       * Is the file writable ?
530       */
531      bool is_writable () const;
532
533    private:
534
535      /**
536       * Begin using the ELF file.
537       *
538       * @param name The full name of the file.
539       * @param fd The file descriptor to read or write the file.
540       * @param writable The file is writeable. It cannot be part of an archive.
541       * @param archive The archive's ELF handle or 0 if not an archive.
542       * @param offset The offset of the ELF file in the archive if elf is non-zero.
543       */
544      void begin (const std::string& name,
545                  int                fd,
546                  const bool         writable,
547                  file*              archive,
548                  off_t              offset);
549
550      /**
551       * Check if the file is usable. Throw an exception if not.
552       *
553       * @param where Where the check is performed.
554       */
555      void check (const char* where) const;
556
557      /**
558       * Check if the file is usable and writable. Throw an exception if not.
559       *
560       * @param where Where the check is performed.
561       */
562      void check_writable (const char* where) const;
563
564      /**
565       * Check if the ELF header is valid. Throw an exception if not.
566       *
567       * @param where Where the check is performed.
568       */
569      void check_ehdr (const char* where) const;
570
571      /**
572       * Check if the ELF program header is valid. Throw an exception if not.
573       *
574       * @param where Where the check is performed.
575       */
576      void check_phdr (const char* where) const;
577
578      /**
579       * Generate libelf error.
580       *
581       * @param where Where the error is generated.
582       */
583      void error (const char* where) const;
584
585      int                  fd_;        //< The file handle.
586      std::string          name_;      //< The name of the file.
587      bool                 archive;    //< The ELF file is part of an archive.
588      bool                 writable;   //< The file is writeable.
589      elf*                 elf_;       //< The ELF handle.
590      unsigned int         mtype;      //< The machine type.
591      unsigned int         oclass;     //< The object class.
592      const char*          ident_str;  //< The ELF file's ident string.
593      size_t               ident_size; //< The size of the ident.
594      elf_ehdr*            ehdr;       //< The ELF header.
595      elf_phdr*            phdr;       //< The ELF program header.
596      section_table        secs;       //< The sections as a table.
597      program_headers      phdrs;      //< The program headers when creating
598                                       //  ELF files.
599      rld::symbols::bucket symbols;    //< The symbols. All tables point here.
600    };
601
602    /**
603     * Return the machine type label given the machine type.
604     *
605     * @param machinetype The ELF machine type.
606     */
607    const std::string machine_type (unsigned int machinetype);
608
609    /**
610     * Return the global machine type set by the check_file call as a string.
611     */
612    const std::string machine_type ();
613
614    /**
615     * Return the global class set by the check_file call.
616     */
617    unsigned int object_class ();
618
619    /**
620     * Return the global machine type set by the check_file call.
621     */
622    unsigned int object_machine_type ();
623
624    /**
625     * Return the global data type set by the check_file call.
626     */
627    unsigned int object_datatype ();
628
629    /**
630     * Check the file against the global machine type, object class and data
631     * type. If this is the first file checked it becomes the default all
632     * others are checked against. This is a simple way to make sure all files
633     * are the same type.
634     *
635     * @param file The check to check.
636     */
637    void check_file(const file& file);
638
639  }
640}
641
642#endif
Note: See TracBrowser for help on using the repository browser.