7 #define DIE do { fprintf(stderr, "ksplice: died at %s:%d\n", __FILE__, __LINE__); abort(); } while(0)
8 #define assert(x) do { if(!(x)) DIE; } while(0)
9 #define align(x, n) ((((x)+(n)-1)/(n))*(n))
11 #define container_of(ptr, type, member) ({ \
12 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
13 (type *)( (char *)__mptr - offsetof(type,member) );})
15 #define DECLARE_VEC_TYPE(elt_t, vectype) \
22 /* void vec_init(struct vectype *vec); */
23 #define vec_init(vec) *(vec) = (typeof(*(vec))) { NULL, 0, 0 }
25 /* void vec_move(struct vectype *dstvec, struct vectype *srcvec); */
26 #define vec_move(dstvec, srcvec) do { \
27 typeof(srcvec) _srcvec = (srcvec); \
28 *(dstvec) = *(_srcvec); \
32 /* void vec_free(struct vectype *vec); */
33 #define vec_free(vec) do { \
34 typeof(vec) _vec1 = (vec); \
39 void vec_do_reserve(void **data
, size_t *mem_size
, size_t newsize
);
41 /* void vec_reserve(struct vectype *vec, size_t new_mem_size); */
42 #define vec_reserve(vec, new_mem_size) do { \
43 typeof(vec) _vec2 = (vec); \
44 vec_do_reserve((void **)&_vec2->data, &_vec2->mem_size, \
48 /* void vec_resize(struct vectype *vec, size_t new_size); */
49 #define vec_resize(vec, new_size) do { \
50 typeof(vec) _vec3 = (vec); \
51 _vec3->size = (new_size); \
52 vec_reserve(_vec3, _vec3->size * sizeof(*_vec3->data)); \
55 /* elt_t *vec_grow(struct vectype *vec, size_t n); */
56 #define vec_grow(vec, n) ({ \
57 typeof(vec) _vec4 = (vec); \
59 vec_resize(_vec4, _vec4->size + _n); \
60 _vec4->data + (_vec4->size - _n); \
63 DECLARE_VEC_TYPE(void, void_vec
);
64 DECLARE_VEC_TYPE(arelent
*, arelentp_vec
);
65 DECLARE_VEC_TYPE(asymbol
*, asymbolp_vec
);
67 #define DECLARE_HASH_TYPE(elt_t, hashtype, \
68 hashtype_init, hashtype_free, \
71 struct bfd_hash_table root; \
74 void hashtype_init(struct hashtype *table); \
75 void hashtype_free(struct hashtype *table); \
76 typeof(elt_t) *hashtype_lookup(struct hashtype *table, \
80 #ifndef BFD_HASH_TABLE_HAS_ENTSIZE
81 #define bfd_hash_table_init(table, newfunc, entry) \
82 bfd_hash_table_init(table, newfunc)
85 #define DEFINE_HASH_TYPE(elt_t, hashtype, \
86 hashtype_init, hashtype_free, \
89 DECLARE_HASH_TYPE(elt_t, hashtype, hashtype_init, \
90 hashtype_free, hashtype_lookup); \
92 struct hashtype##_entry { \
93 struct bfd_hash_entry root; \
97 static struct bfd_hash_entry *hashtype##_newfunc( \
98 struct bfd_hash_entry *entry, \
99 struct bfd_hash_table *table, \
100 const char *string) \
102 if (entry == NULL) { \
103 entry = bfd_hash_allocate(table, \
104 sizeof(struct hashtype##_entry)); \
108 entry = bfd_hash_newfunc(entry, table, string); \
110 &container_of(entry, struct hashtype##_entry, \
116 void hashtype_init(struct hashtype *table) \
118 bfd_hash_table_init(&table->root, hashtype##_newfunc, \
119 sizeof(struct hashtype##_entry)); \
122 void hashtype_free(struct hashtype *table) \
124 bfd_hash_table_free(&table->root); \
127 typeof(elt_t) *hashtype_lookup(struct hashtype *table, \
128 const char *string, \
129 bfd_boolean create) \
131 struct bfd_hash_entry *e = \
132 bfd_hash_lookup(&table->root, string, create, \
136 else if (e == NULL) \
138 return &container_of(e, struct hashtype##_entry, \
142 struct eat_trailing_semicolon
144 #ifndef bfd_get_section_size
145 #define bfd_get_section_size(x) ((x)->_cooked_size)
151 struct asymbolp_vec syms
;
152 struct supersect
*new_supersects
;
156 struct superbfd
*parent
;
159 struct void_vec contents
;
161 struct arelentp_vec relocs
;
162 struct arelentp_vec new_relocs
;
163 struct supersect
*next
;
167 struct kernel_symbol
{
172 struct superbfd
*fetch_superbfd(bfd
*abfd
);
173 struct supersect
*fetch_supersect(struct superbfd
*sbfd
, asection
*sect
);
174 struct supersect
*new_supersect(struct superbfd
*sbfd
, const char *name
);
175 void supersect_move(struct supersect
*dest_ss
, struct supersect
*src_ss
);
177 #define sect_grow(ss, n, type) \
178 ((type *)sect_do_grow(ss, n, sizeof(type), __alignof__(type)))
179 void *sect_do_grow(struct supersect
*ss
, size_t n
, size_t size
, int alignment
);
181 #define sect_copy(dest_ss, dest, src_ss, src, n) \
182 sect_do_copy(dest_ss, dest, src_ss, src, (n) * sizeof(*(src)))
183 void sect_do_copy(struct supersect
*dest_ss
, void *dest
,
184 struct supersect
*src_ss
, const void *src
, size_t n
);
186 #define starts_with(str, prefix) \
187 (strncmp(str, prefix, strlen(prefix)) == 0)
188 #define ends_with(str, suffix) \
189 (strlen(str) >= strlen(suffix) && \
190 strcmp(&str[strlen(str) - strlen(suffix)], suffix) == 0)
192 bfd_vma
addr_offset(struct supersect
*ss
, const void *addr
);
193 bfd_vma
get_reloc_offset(struct supersect
*ss
, arelent
*reloc
, int adjust_pc
);
194 bfd_vma
read_reloc(struct supersect
*ss
, const void *addr
, size_t size
,
196 const void *read_pointer(struct supersect
*ss
, void *const *addr
,
197 struct supersect
**ssp
);
198 const char *read_string(struct supersect
*ss
, const char *const *addr
);
199 char *str_pointer(struct supersect
*ss
, void *const *addr
);
201 #define read_num(ss, addr) ((typeof(*(addr))) \
202 read_reloc(ss, addr, sizeof(*(addr)), NULL))