home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1999 May / pcp151c.iso / misc / src / install / modutils / include / obj.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-06  |  5.2 KB  |  196 lines

  1. /* Elf object file loading and relocation routines.
  2.    Copyright 1996, 1997 Linux International.
  3.  
  4.    Contributed by Richard Henderson <rth@tamu.edu>
  5.  
  6.    This file is part of the Linux modutils.
  7.  
  8.    This program is free software; you can redistribute it and/or modify it
  9.    under the terms of the GNU General Public License as published by the
  10.    Free Software Foundation; either version 2 of the License, or (at your
  11.    option) any later version.
  12.  
  13.    This program is distributed in the hope that it will be useful, but
  14.    WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.    General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with this program; if not, write to the Free Software Foundation,
  20.    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  21.  
  22.  
  23. #ifndef MODUTILS_OBJ_H
  24. #define MODUTILS_OBJ_H 1
  25.  
  26. #ident "$Id: obj.h,v 1.1.1.1 1998/01/06 20:51:07 ewt Exp $"
  27.  
  28. /* The relocatable object is manipulated using elfin types.  */
  29.  
  30. #include <stdio.h>
  31. #include <elf.h>
  32. #include ELF_MACHINE_H
  33.  
  34. #ifndef ElfW
  35. # if ELFCLASSM == ELFCLASS32
  36. #  define ElfW(x)  Elf32_ ## x
  37. #  define ELFW(x)  ELF32_ ## x
  38. # else
  39. #  define ElfW(x)  Elf64_ ## x
  40. #  define ELFW(x)  ELF64_ ## x
  41. # endif
  42. #endif
  43.  
  44. /* For some reason this is missing from lib5.  */
  45. #ifndef ELF32_ST_INFO
  46. # define ELF32_ST_INFO(bind, type)       (((bind) << 4) + ((type) & 0xf))
  47. #endif
  48.  
  49. #ifndef ELF64_ST_INFO
  50. # define ELF64_ST_INFO(bind, type)       (((bind) << 4) + ((type) & 0xf))
  51. #endif
  52.  
  53. struct obj_string_patch;
  54. struct obj_symbol_patch;
  55.  
  56. struct obj_section
  57. {
  58.   ElfW(Shdr) header;
  59.   const char *name;
  60.   char *contents;
  61.   struct obj_section *load_next;
  62.   int idx;
  63. };
  64.  
  65. struct obj_symbol
  66. {
  67.   struct obj_symbol *next;    /* hash table link */
  68.   const char *name;
  69.   unsigned long value;
  70.   unsigned long size;
  71.   int secidx;            /* the defining section index/module */
  72.   int info;
  73.   int ksymidx;            /* for export to the kernel symtab */
  74.   int referenced;        /* actually used in the link */
  75. };
  76.  
  77. /* Hardcode the hash table size.  We shouldn't be needing so many
  78.    symbols that we begin to degrade performance, and we get a big win
  79.    by giving the compiler a constant divisor.  */
  80.  
  81. #define HASH_BUCKETS  521
  82.  
  83. struct obj_file
  84. {
  85.   ElfW(Ehdr) header;
  86.   ElfW(Addr) baseaddr;
  87.   struct obj_section **sections;
  88.   struct obj_section *load_order;
  89.   struct obj_section **load_order_search_start;
  90.   struct obj_string_patch *string_patches;
  91.   struct obj_symbol_patch *symbol_patches;
  92.   int (*symbol_cmp)(const char *, const char *);
  93.   unsigned long (*symbol_hash)(const char *);
  94.   struct obj_symbol *symtab[HASH_BUCKETS];
  95. };
  96.  
  97. enum obj_reloc
  98. {
  99.   obj_reloc_ok,
  100.   obj_reloc_overflow,
  101.   obj_reloc_dangerous,
  102.   obj_reloc_unhandled
  103. };
  104.  
  105. struct obj_string_patch
  106. {
  107.   struct obj_string_patch *next;
  108.   int reloc_secidx;
  109.   ElfW(Addr) reloc_offset;
  110.   ElfW(Addr) string_offset;
  111. };
  112.  
  113. struct obj_symbol_patch
  114. {
  115.   struct obj_symbol_patch *next;
  116.   int reloc_secidx;
  117.   ElfW(Addr) reloc_offset;
  118.   struct obj_symbol *sym;
  119. };
  120.  
  121.  
  122. /* Generic object manipulation routines.  */
  123.  
  124. unsigned long obj_elf_hash(const char *);
  125.  
  126. unsigned long obj_elf_hash_n(const char *, unsigned long len);
  127.  
  128. struct obj_symbol *obj_add_symbol (struct obj_file *f, const char *name,
  129.                    int info, int secidx,
  130.                    ElfW(Addr) value, unsigned long size);
  131.  
  132. struct obj_symbol *obj_find_symbol (struct obj_file *f,
  133.                      const char *name);
  134.  
  135. ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
  136.                   struct obj_symbol *sym);
  137.  
  138. void obj_set_symbol_compare(struct obj_file *f,
  139.                 int (*cmp)(const char *, const char *),
  140.                 unsigned long (*hash)(const char *));
  141.  
  142. struct obj_section *obj_find_section (struct obj_file *f,
  143.                        const char *name);
  144.  
  145. void obj_insert_section_load_order (struct obj_file *f,
  146.                     struct obj_section *sec);
  147.  
  148. struct obj_section *obj_create_alloced_section (struct obj_file *f,
  149.                         const char *name,
  150.                         unsigned long align,
  151.                         unsigned long size);
  152.  
  153. struct obj_section *obj_create_alloced_section_first (struct obj_file *f,
  154.                               const char *name,
  155.                               unsigned long align,
  156.                               unsigned long size);
  157.  
  158. void *obj_extend_section (struct obj_section *sec, unsigned long more);
  159.  
  160. int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
  161.              const char *string);
  162.  
  163. int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
  164.              struct obj_symbol *sym);
  165.  
  166. int obj_allocate_commons_and_check_undefineds(struct obj_file *f);
  167.  
  168. unsigned long obj_load_size (struct obj_file *f);
  169.  
  170. int obj_relocate (struct obj_file *f, ElfW(Addr) base);
  171.  
  172. struct obj_file *obj_load(FILE *f);
  173.  
  174. int obj_create_image (struct obj_file *f, char *image);
  175.  
  176. /* Architecture specific manipulation routines.  */
  177.  
  178. struct obj_file *arch_new_file (void);
  179.  
  180. struct obj_section *arch_new_section (void);
  181.  
  182. struct obj_symbol *arch_new_symbol (void);
  183.  
  184. enum obj_reloc arch_apply_relocation (struct obj_file *f,
  185.                       struct obj_section *targsec,
  186.                       struct obj_section *symsec,
  187.                       struct obj_symbol *sym,
  188.                       ElfW(RelM) *rel, ElfW(Addr) value);
  189.  
  190. int arch_create_got (struct obj_file *f);
  191.  
  192. struct new_module;
  193. int arch_init_module (struct obj_file *f, struct new_module *);
  194.  
  195. #endif /* obj.h */
  196.