8 #define DIE do { fprintf(stderr, "ksplice: died at %s:%d\n", __FILE__, __LINE__); abort(); } while(0)
9 #define assert(x) do { if(!(x)) DIE; } while(0)
10 #define align(x, n) ((((x)+(n)-1)/(n))*(n))
12 #define container_of(ptr, type, member) ({ \
13 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
14 (type *)( (char *)__mptr - offsetof(type,member) );})
16 #define DECLARE_VEC_TYPE(elt_t, vectype) \
23 /* void vec_init(struct vectype *vec); */
24 #define vec_init(vec) *(vec) = (typeof(*(vec))) { NULL, 0, 0 }
26 /* void vec_move(struct vectype *dstvec, struct vectype *srcvec); */
27 #define vec_move(dstvec, srcvec) do { \
28 typeof(srcvec) _srcvec = (srcvec); \
29 *(dstvec) = *(_srcvec); \
33 /* void vec_free(struct vectype *vec); */
34 #define vec_free(vec) do { \
35 typeof(vec) _vec1 = (vec); \
40 void vec_do_reserve(void **data
, size_t *mem_size
, size_t newsize
);
42 /* void vec_reserve(struct vectype *vec, size_t new_mem_size); */
43 #define vec_reserve(vec, new_mem_size) do { \
44 typeof(vec) _vec2 = (vec); \
45 vec_do_reserve((void **)&_vec2->data, &_vec2->mem_size, \
49 /* void vec_resize(struct vectype *vec, size_t new_size); */
50 #define vec_resize(vec, new_size) do { \
51 typeof(vec) _vec3 = (vec); \
52 _vec3->size = (new_size); \
53 vec_reserve(_vec3, _vec3->size * sizeof(*_vec3->data)); \
56 /* elt_t *vec_grow(struct vectype *vec, size_t n); */
57 #define vec_grow(vec, n) ({ \
58 typeof(vec) _vec4 = (vec); \
60 vec_resize(_vec4, _vec4->size + _n); \
61 _vec4->data + (_vec4->size - _n); \
64 DECLARE_VEC_TYPE(void, void_vec
);
65 DECLARE_VEC_TYPE(arelent
*, arelentp_vec
);
66 DECLARE_VEC_TYPE(asymbol
*, asymbolp_vec
);
68 #define DECLARE_HASH_TYPE(elt_t, hashtype, \
69 hashtype_init, hashtype_free, \
72 struct bfd_hash_table root; \
75 void hashtype_init(struct hashtype *table); \
76 void hashtype_free(struct hashtype *table); \
77 typeof(elt_t) *hashtype_lookup(struct hashtype *table, \
81 #ifndef BFD_HASH_TABLE_HAS_ENTSIZE
82 #define bfd_hash_table_init(table, newfunc, entry) \
83 bfd_hash_table_init(table, newfunc)
86 #define DEFINE_HASH_TYPE(elt_t, hashtype, \
87 hashtype_init, hashtype_free, \
90 DECLARE_HASH_TYPE(elt_t, hashtype, hashtype_init, \
91 hashtype_free, hashtype_lookup); \
93 struct hashtype##_entry { \
94 struct bfd_hash_entry root; \
98 static struct bfd_hash_entry *hashtype##_newfunc( \
99 struct bfd_hash_entry *entry, \
100 struct bfd_hash_table *table, \
101 const char *string) \
103 if (entry == NULL) { \
104 entry = bfd_hash_allocate(table, \
105 sizeof(struct hashtype##_entry)); \
109 entry = bfd_hash_newfunc(entry, table, string); \
111 &container_of(entry, struct hashtype##_entry, \
117 void hashtype_init(struct hashtype *table) \
119 bfd_hash_table_init(&table->root, hashtype##_newfunc, \
120 sizeof(struct hashtype##_entry)); \
123 void hashtype_free(struct hashtype *table) \
125 bfd_hash_table_free(&table->root); \
128 typeof(elt_t) *hashtype_lookup(struct hashtype *table, \
129 const char *string, \
130 bfd_boolean create) \
132 struct bfd_hash_entry *e = \
133 bfd_hash_lookup(&table->root, string, create, \
137 else if (e == NULL) \
139 return &container_of(e, struct hashtype##_entry, \
143 struct eat_trailing_semicolon
145 #ifndef bfd_get_section_size
146 #define bfd_get_section_size(x) ((x)->_cooked_size)
151 const char *orig_label
;
156 DECLARE_VEC_TYPE(struct label_map
, label_map_vec
);
160 struct asymbolp_vec syms
;
161 struct supersect
*new_supersects
;
162 struct label_map_vec maps
;
166 struct superbfd
*parent
;
169 struct void_vec contents
;
171 struct arelentp_vec relocs
;
172 struct arelentp_vec new_relocs
;
173 struct supersect
*next
;
174 struct asymbolp_vec syms
;
178 struct kernel_symbol
{
183 struct superbfd
*fetch_superbfd(bfd
*abfd
);
184 struct supersect
*fetch_supersect(struct superbfd
*sbfd
, asection
*sect
);
185 struct supersect
*new_supersect(struct superbfd
*sbfd
, const char *name
);
186 void supersect_move(struct supersect
*dest_ss
, struct supersect
*src_ss
);
188 #define sect_grow(ss, n, type) \
189 ((type *)sect_do_grow(ss, n, sizeof(type), __alignof__(type)))
190 void *sect_do_grow(struct supersect
*ss
, size_t n
, size_t size
, int alignment
);
192 #define sect_copy(dest_ss, dest, src_ss, src, n) \
193 sect_do_copy(dest_ss, dest, src_ss, src, (n) * sizeof(*(src)))
194 void sect_do_copy(struct supersect
*dest_ss
, void *dest
,
195 struct supersect
*src_ss
, const void *src
, size_t n
);
197 #define starts_with(str, prefix) \
198 (strncmp(str, prefix, strlen(prefix)) == 0)
199 #define ends_with(str, suffix) \
200 (strlen(str) >= strlen(suffix) && \
201 strcmp(&str[strlen(str) - strlen(suffix)], suffix) == 0)
203 bfd_vma
addr_offset(struct supersect
*ss
, const void *addr
);
204 bfd_vma
get_reloc_offset(struct supersect
*ss
, arelent
*reloc
, bool adjust_pc
);
205 bfd_vma
read_reloc(struct supersect
*ss
, const void *addr
, size_t size
,
207 const void *read_pointer(struct supersect
*ss
, void *const *addr
,
208 struct supersect
**ssp
);
209 const char *read_string(struct supersect
*ss
, const char *const *addr
);
210 char *str_pointer(struct supersect
*ss
, void *const *addr
);
212 #define read_num(ss, addr) ((typeof(*(addr))) \
213 read_reloc(ss, addr, sizeof(*(addr)), NULL))
215 asymbol
*canonical_symbol(struct superbfd
*sbfd
, asymbol
*sym
);
216 asymbol
**canonical_symbolp(struct superbfd
*sbfd
, asymbol
*sym
);
217 const char *label_lookup(struct superbfd
*sbfd
, asymbol
*sym
);
218 void read_label_map(struct superbfd
*sbfd
);
220 bool is_special(asection
*sect
);