Rename activate_primary to finalize_pack.
[ksplice.git] / objcommon.h
blobc72b34ddb7c28508617afcbc24206bdc7407a408
1 #include <bfd.h>
2 #include <stdbool.h>
3 #include <stddef.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
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) \
17 struct vectype { \
18 elt_t *data; \
19 size_t size; \
20 size_t mem_size; \
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); \
30 vec_init(_srcvec); \
31 } while (0)
33 /* void vec_free(struct vectype *vec); */
34 #define vec_free(vec) do { \
35 typeof(vec) _vec1 = (vec); \
36 free(_vec1->data); \
37 vec_init(_vec1); \
38 } while (0)
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, \
46 (new_mem_size)); \
47 } while (0)
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)); \
54 } while (0)
56 /* elt_t *vec_grow(struct vectype *vec, size_t n); */
57 #define vec_grow(vec, n) ({ \
58 typeof(vec) _vec4 = (vec); \
59 size_t _n = (n); \
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, \
70 hashtype_lookup) \
71 struct hashtype { \
72 struct bfd_hash_table root; \
73 }; \
75 void hashtype_init(struct hashtype *table); \
76 void hashtype_free(struct hashtype *table); \
77 typeof(elt_t) *hashtype_lookup(struct hashtype *table, \
78 const char *string, \
79 bfd_boolean create)
81 #ifndef BFD_HASH_TABLE_HAS_ENTSIZE
82 #define bfd_hash_table_init(table, newfunc, entry) \
83 bfd_hash_table_init(table, newfunc)
84 #endif
86 #define DEFINE_HASH_TYPE(elt_t, hashtype, \
87 hashtype_init, hashtype_free, \
88 hashtype_lookup, \
89 elt_construct) \
90 DECLARE_HASH_TYPE(elt_t, hashtype, hashtype_init, \
91 hashtype_free, hashtype_lookup); \
93 struct hashtype##_entry { \
94 struct bfd_hash_entry root; \
95 typeof(elt_t) val; \
96 }; \
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)); \
106 if (entry == NULL) \
107 return entry; \
109 entry = bfd_hash_newfunc(entry, table, string); \
110 typeof(elt_t) *v = \
111 &container_of(entry, struct hashtype##_entry, \
112 root)->val; \
113 elt_construct(v); \
114 return entry; \
115 }; \
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, \
134 TRUE); \
135 if (create) \
136 assert(e != NULL); \
137 else if (e == NULL) \
138 return NULL; \
139 return &container_of(e, struct hashtype##_entry, \
140 root)->val; \
143 struct eat_trailing_semicolon
145 #ifndef bfd_get_section_size
146 #define bfd_get_section_size(x) ((x)->_cooked_size)
147 #endif
149 struct label_map {
150 asymbol *csym;
151 const char *orig_label;
152 const char *label;
153 int count;
154 int index;
156 DECLARE_VEC_TYPE(struct label_map, label_map_vec);
158 struct superbfd {
159 bfd *abfd;
160 struct asymbolp_vec syms;
161 struct supersect *new_supersects;
162 struct label_map_vec maps;
165 struct supersect {
166 struct superbfd *parent;
167 const char *name;
168 flagword flags;
169 struct void_vec contents;
170 int alignment;
171 struct arelentp_vec relocs;
172 struct arelentp_vec new_relocs;
173 struct supersect *next;
174 struct asymbolp_vec syms;
175 asymbol *symbol;
178 struct kernel_symbol {
179 unsigned long value;
180 char *name;
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,
206 asymbol **symp);
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);