Print out the section types of sections that can't be matched.
[ksplice.git] / objcommon.h
blob8453645b635c7172cc24b82d93e602e940ff3e69
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);
70 DECLARE_VEC_TYPE(asymbol **, asymbolpp_vec);
72 #define DECLARE_HASH_TYPE(elt_t, hashtype, \
73 hashtype_init, hashtype_free, \
74 hashtype_lookup) \
75 struct hashtype { \
76 struct bfd_hash_table root; \
77 }; \
79 void hashtype_init(struct hashtype *table); \
80 void hashtype_free(struct hashtype *table); \
81 typeof(elt_t) *hashtype_lookup(struct hashtype *table, \
82 const char *string, \
83 bfd_boolean create)
85 #ifndef BFD_HASH_TABLE_HAS_ENTSIZE
86 #define bfd_hash_table_init(table, newfunc, entry) \
87 bfd_hash_table_init(table, newfunc)
88 #endif
90 #define DEFINE_HASH_TYPE(elt_t, hashtype, \
91 hashtype_init, hashtype_free, \
92 hashtype_lookup, \
93 elt_construct) \
94 DECLARE_HASH_TYPE(elt_t, hashtype, hashtype_init, \
95 hashtype_free, hashtype_lookup); \
97 struct hashtype##_entry { \
98 struct bfd_hash_entry root; \
99 typeof(elt_t) val; \
100 }; \
102 static struct bfd_hash_entry *hashtype##_newfunc( \
103 struct bfd_hash_entry *entry, \
104 struct bfd_hash_table *table, \
105 const char *string) \
107 if (entry == NULL) { \
108 entry = bfd_hash_allocate(table, \
109 sizeof(struct hashtype##_entry)); \
110 if (entry == NULL) \
111 return entry; \
113 entry = bfd_hash_newfunc(entry, table, string); \
114 typeof(elt_t) *v = \
115 &container_of(entry, struct hashtype##_entry, \
116 root)->val; \
117 elt_construct(v); \
118 return entry; \
119 }; \
121 void hashtype_init(struct hashtype *table) \
123 bfd_hash_table_init(&table->root, hashtype##_newfunc, \
124 sizeof(struct hashtype##_entry)); \
127 void hashtype_free(struct hashtype *table) \
129 bfd_hash_table_free(&table->root); \
132 typeof(elt_t) *hashtype_lookup(struct hashtype *table, \
133 const char *string, \
134 bfd_boolean create) \
136 struct bfd_hash_entry *e = \
137 bfd_hash_lookup(&table->root, string, create, \
138 TRUE); \
139 if (create) \
140 assert(e != NULL); \
141 else if (e == NULL) \
142 return NULL; \
143 return &container_of(e, struct hashtype##_entry, \
144 root)->val; \
147 struct eat_trailing_semicolon
149 #ifndef bfd_get_section_size
150 #define bfd_get_section_size(x) ((x)->_cooked_size)
151 #endif
153 struct label_map {
154 asymbol *csym;
155 const char *orig_label;
156 const char *label;
157 int count;
158 int index;
160 DECLARE_VEC_TYPE(struct label_map, label_map_vec);
162 struct span {
163 struct supersect *ss;
164 asymbol *symbol;
165 const char *label;
166 bfd_vma start;
167 bfd_vma size;
168 bool keep;
169 bool new;
170 bfd_size_type shift;
172 DECLARE_VEC_TYPE(struct span, span_vec);
174 struct superbfd {
175 bfd *abfd;
176 struct asymbolp_vec syms;
177 struct supersect *new_supersects;
178 struct label_map_vec maps;
179 struct asymbolpp_vec new_syms;
182 enum supersect_type {
183 SS_TYPE_TEXT, SS_TYPE_DATA, SS_TYPE_RODATA, SS_TYPE_STRING,
184 SS_TYPE_SPECIAL, SS_TYPE_IGNORED, SS_TYPE_KSPLICE, SS_TYPE_EXPORT,
185 SS_TYPE_UNKNOWN
188 struct supersect {
189 struct superbfd *parent;
190 const char *name;
191 flagword flags;
192 struct void_vec contents;
193 int alignment;
194 struct arelentp_vec relocs;
195 struct arelentp_vec new_relocs;
196 struct supersect *next;
197 struct asymbolp_vec syms;
198 struct span_vec spans;
199 asymbol *symbol;
200 struct supersect *match;
201 bool new;
202 bool patch;
203 bool keep;
204 enum supersect_type type;
207 struct kernel_symbol {
208 unsigned long value;
209 char *name;
212 struct superbfd *fetch_superbfd(bfd *abfd);
213 struct supersect *fetch_supersect(struct superbfd *sbfd, asection *sect);
214 struct supersect *new_supersect(struct superbfd *sbfd, const char *name);
215 void supersect_move(struct supersect *dest_ss, struct supersect *src_ss);
217 #define sect_grow(ss, n, type) \
218 ((type *)sect_do_grow(ss, n, sizeof(type), __alignof__(type)))
219 void *sect_do_grow(struct supersect *ss, size_t n, size_t size, int alignment);
221 #define sect_copy(dest_ss, dest, src_ss, src, n) \
222 sect_do_copy(dest_ss, dest, src_ss, src, (n) * sizeof(*(src)))
223 void sect_do_copy(struct supersect *dest_ss, void *dest,
224 struct supersect *src_ss, const void *src, size_t n);
226 #define starts_with(str, prefix) \
227 (strncmp(str, prefix, strlen(prefix)) == 0)
228 #define ends_with(str, suffix) \
229 (strlen(str) >= strlen(suffix) && \
230 strcmp(&str[strlen(str) - strlen(suffix)], suffix) == 0)
232 bfd_vma addr_offset(struct supersect *ss, const void *addr);
233 bfd_vma get_reloc_offset(struct supersect *ss, arelent *reloc, bool adjust_pc);
234 bfd_vma read_reloc(struct supersect *ss, const void *addr, size_t size,
235 asymbol **symp);
236 const void *read_pointer(struct supersect *ss, void *const *addr,
237 struct supersect **ssp);
238 const char *read_string(struct supersect *ss, const char *const *addr);
239 char *str_pointer(struct supersect *ss, void *const *addr);
241 #define read_num(ss, addr) ((typeof(*(addr))) \
242 read_reloc(ss, addr, sizeof(*(addr)), NULL))
244 asymbol **symbolp_scan(struct superbfd *sbfd, asection *sect, bfd_vma value);
245 asymbol *canonical_symbol(struct superbfd *sbfd, asymbol *sym);
246 asymbol **canonical_symbolp(struct superbfd *sbfd, asymbol *sym);
247 char *static_local_symbol(struct superbfd *sbfd, asymbol *sym);
248 char *symbol_label(struct superbfd *sbfd, asymbol *sym);