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

4.104.115
Last change on this file since 5825009 was 42f766f, checked in by Chris Johns <chrisj@…>, on 12/12/12 at 10:51:03

Relocation fixes.

These changes implement a suitable relocation output in the image.
The code is still not working 100% but these changes are a big
improvement.

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