Use print_label_map to pass the label_map from objdiff to objmanip.
[ksplice.git] / objcommon.h
blob794c34897e502d0670d7455e6affb466a3644d26
1 #include <bfd.h>
2 #include <limits.h>
3 #include <stdbool.h>
4 #include <stddef.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
9 #define BITS_PER_LONG LONG_BIT
11 #define DIE do { fprintf(stderr, "ksplice: died at %s:%d\n", __FILE__, __LINE__); abort(); } while(0)
12 #define assert(x) do { if(!(x)) DIE; } while(0)
13 #define align(x, n) ((((x)+(n)-1)/(n))*(n))
15 #define container_of(ptr, type, member) ({ \
16 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
17 (type *)( (char *)__mptr - offsetof(type,member) );})
19 #define DECLARE_VEC_TYPE(elt_t, vectype) \
20 struct vectype { \
21 elt_t *data; \
22 size_t size; \
23 size_t mem_size; \
26 /* void vec_init(struct vectype *vec); */
27 #define vec_init(vec) *(vec) = (typeof(*(vec))) { NULL, 0, 0 }
29 /* void vec_move(struct vectype *dstvec, struct vectype *srcvec); */
30 #define vec_move(dstvec, srcvec) do { \
31 typeof(srcvec) _srcvec = (srcvec); \
32 *(dstvec) = *(_srcvec); \
33 vec_init(_srcvec); \
34 } while (0)
36 /* void vec_free(struct vectype *vec); */
37 #define vec_free(vec) do { \
38 typeof(vec) _vec1 = (vec); \
39 free(_vec1->data); \
40 vec_init(_vec1); \
41 } while (0)
43 void vec_do_reserve(void **data, size_t *mem_size, size_t newsize);
45 /* void vec_reserve(struct vectype *vec, size_t new_mem_size); */
46 #define vec_reserve(vec, new_mem_size) do { \
47 typeof(vec) _vec2 = (vec); \
48 vec_do_reserve((void **)&_vec2->data, &_vec2->mem_size, \
49 (new_mem_size)); \
50 } while (0)
52 /* void vec_resize(struct vectype *vec, size_t new_size); */
53 #define vec_resize(vec, new_size) do { \
54 typeof(vec) _vec3 = (vec); \
55 _vec3->size = (new_size); \
56 vec_reserve(_vec3, _vec3->size * sizeof(*_vec3->data)); \
57 } while (0)
59 /* elt_t *vec_grow(struct vectype *vec, size_t n); */
60 #define vec_grow(vec, n) ({ \
61 typeof(vec) _vec4 = (vec); \
62 size_t _n = (n); \
63 vec_resize(_vec4, _vec4->size + _n); \
64 _vec4->data + (_vec4->size - _n); \
67 DECLARE_VEC_TYPE(void, void_vec);
68 DECLARE_VEC_TYPE(arelent *, arelentp_vec);
69 DECLARE_VEC_TYPE(asymbol *, asymbolp_vec);
71 #define DECLARE_HASH_TYPE(elt_t, hashtype, \
72 hashtype_init, hashtype_free, \
73 hashtype_lookup) \
74 struct hashtype { \
75 struct bfd_hash_table root; \
76 }; \
78 void hashtype_init(struct hashtype *table); \
79 void hashtype_free(struct hashtype *table); \
80 typeof(elt_t) *hashtype_lookup(struct hashtype *table, \
81 const char *string, \
82 bfd_boolean create)
84 #ifndef BFD_HASH_TABLE_HAS_ENTSIZE
85 #define bfd_hash_table_init(table, newfunc, entry) \
86 bfd_hash_table_init(table, newfunc)
87 #endif
89 #define DEFINE_HASH_TYPE(elt_t, hashtype, \
90 hashtype_init, hashtype_free, \
91 hashtype_lookup, \
92 elt_construct) \
93 DECLARE_HASH_TYPE(elt_t, hashtype, hashtype_init, \
94 hashtype_free, hashtype_lookup); \
96 struct hashtype##_entry { \
97 struct bfd_hash_entry root; \
98 typeof(elt_t) val; \
99 }; \
101 static struct bfd_hash_entry *hashtype##_newfunc( \
102 struct bfd_hash_entry *entry, \
103 struct bfd_hash_table *table, \
104 const char *string) \
106 if (entry == NULL) { \
107 entry = bfd_hash_allocate(table, \
108 sizeof(struct hashtype##_entry)); \
109 if (entry == NULL) \
110 return entry; \
112 entry = bfd_hash_newfunc(entry, table, string); \
113 typeof(elt_t) *v = \
114 &container_of(entry, struct hashtype##_entry, \
115 root)->val; \
116 elt_construct(v); \
117 return entry; \
118 }; \
120 void hashtype_init(struct hashtype *table) \
122 bfd_hash_table_init(&table->root, hashtype##_newfunc, \
123 sizeof(struct hashtype##_entry)); \
126 void hashtype_free(struct hashtype *table) \
128 bfd_hash_table_free(&table->root); \
131 typeof(elt_t) *hashtype_lookup(struct hashtype *table, \
132 const char *string, \
133 bfd_boolean create) \
135 struct bfd_hash_entry *e = \
136 bfd_hash_lookup(&table->root, string, create, \
137 TRUE); \
138 if (create) \
139 assert(e != NULL); \
140 else if (e == NULL) \
141 return NULL; \
142 return &container_of(e, struct hashtype##_entry, \
143 root)->val; \
146 struct eat_trailing_semicolon
148 #ifndef bfd_get_section_size
149 #define bfd_get_section_size(x) ((x)->_cooked_size)
150 #endif
152 struct label_map {
153 asymbol *csym;
154 const char *orig_label;
155 const char *label;
156 int count;
157 int index;
159 DECLARE_VEC_TYPE(struct label_map, label_map_vec);
161 struct superbfd {
162 bfd *abfd;
163 struct asymbolp_vec syms;
164 struct supersect *new_supersects;
165 struct label_map_vec maps;
168 struct supersect {
169 struct superbfd *parent;
170 const char *name;
171 flagword flags;
172 struct void_vec contents;
173 int alignment;
174 struct arelentp_vec relocs;
175 struct arelentp_vec new_relocs;
176 struct supersect *next;
177 struct asymbolp_vec syms;
178 asymbol *symbol;
181 struct kernel_symbol {
182 unsigned long value;
183 char *name;
186 struct exception_table_entry {
187 unsigned long insn, fixup;
190 struct superbfd *fetch_superbfd(bfd *abfd);
191 struct supersect *fetch_supersect(struct superbfd *sbfd, asection *sect);
192 struct supersect *new_supersect(struct superbfd *sbfd, const char *name);
193 void supersect_move(struct supersect *dest_ss, struct supersect *src_ss);
195 #define sect_grow(ss, n, type) \
196 ((type *)sect_do_grow(ss, n, sizeof(type), __alignof__(type)))
197 void *sect_do_grow(struct supersect *ss, size_t n, size_t size, int alignment);
199 #define sect_copy(dest_ss, dest, src_ss, src, n) \
200 sect_do_copy(dest_ss, dest, src_ss, src, (n) * sizeof(*(src)))
201 void sect_do_copy(struct supersect *dest_ss, void *dest,
202 struct supersect *src_ss, const void *src, size_t n);
204 #define starts_with(str, prefix) \
205 (strncmp(str, prefix, strlen(prefix)) == 0)
206 #define ends_with(str, suffix) \
207 (strlen(str) >= strlen(suffix) && \
208 strcmp(&str[strlen(str) - strlen(suffix)], suffix) == 0)
210 bfd_vma addr_offset(struct supersect *ss, const void *addr);
211 bfd_vma get_reloc_offset(struct supersect *ss, arelent *reloc, bool adjust_pc);
212 bfd_vma read_reloc(struct supersect *ss, const void *addr, size_t size,
213 asymbol **symp);
214 const void *read_pointer(struct supersect *ss, void *const *addr,
215 struct supersect **ssp);
216 const char *read_string(struct supersect *ss, const char *const *addr);
217 char *str_pointer(struct supersect *ss, void *const *addr);
219 #define read_num(ss, addr) ((typeof(*(addr))) \
220 read_reloc(ss, addr, sizeof(*(addr)), NULL))
222 asymbol *canonical_symbol(struct superbfd *sbfd, asymbol *sym);
223 asymbol **canonical_symbolp(struct superbfd *sbfd, asymbol *sym);
224 const char *label_lookup(struct superbfd *sbfd, asymbol *sym);
225 void print_label_map(struct superbfd *sbfd);
226 void read_label_map(struct superbfd *sbfd);
227 void label_map_set(struct superbfd *sbfd, const char *oldlabel,
228 const char *label);
230 bool is_special(asection *sect);