Don't require a trailing period on .rodata and .data.
[ksplice.git] / objcommon.h
blob28edb2d43576fb4006cd05440efedeae34e3b491
1 #include <bfd.h>
2 #include <stddef.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
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) \
16 struct vectype { \
17 elt_t *data; \
18 size_t size; \
19 size_t mem_size; \
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); \
29 vec_init(_srcvec); \
30 } while (0)
32 /* void vec_free(struct vectype *vec); */
33 #define vec_free(vec) do { \
34 typeof(vec) _vec1 = (vec); \
35 free(_vec1->data); \
36 vec_init(_vec1); \
37 } while (0)
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, \
45 (new_mem_size)); \
46 } while (0)
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)); \
53 } while (0)
55 /* elt_t *vec_grow(struct vectype *vec, size_t n); */
56 #define vec_grow(vec, n) ({ \
57 typeof(vec) _vec4 = (vec); \
58 size_t _n = (n); \
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, \
69 hashtype_lookup) \
70 struct hashtype { \
71 struct bfd_hash_table root; \
72 }; \
74 void hashtype_init(struct hashtype *table); \
75 void hashtype_free(struct hashtype *table); \
76 typeof(elt_t) *hashtype_lookup(struct hashtype *table, \
77 const char *string, \
78 bfd_boolean create)
80 #ifndef BFD_HASH_TABLE_HAS_ENTSIZE
81 #define bfd_hash_table_init(table, newfunc, entry) \
82 bfd_hash_table_init(table, newfunc)
83 #endif
85 #define DEFINE_HASH_TYPE(elt_t, hashtype, \
86 hashtype_init, hashtype_free, \
87 hashtype_lookup, \
88 elt_construct) \
89 DECLARE_HASH_TYPE(elt_t, hashtype, hashtype_init, \
90 hashtype_free, hashtype_lookup); \
92 struct hashtype##_entry { \
93 struct bfd_hash_entry root; \
94 typeof(elt_t) val; \
95 }; \
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)); \
105 if (entry == NULL) \
106 return entry; \
108 entry = bfd_hash_newfunc(entry, table, string); \
109 typeof(elt_t) *v = \
110 &container_of(entry, struct hashtype##_entry, \
111 root)->val; \
112 elt_construct(v); \
113 return entry; \
114 }; \
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, \
133 TRUE); \
134 if (create) \
135 assert(e != NULL); \
136 else if (e == NULL) \
137 return NULL; \
138 return &container_of(e, struct hashtype##_entry, \
139 root)->val; \
142 struct eat_trailing_semicolon
144 #ifndef bfd_get_section_size
145 #define bfd_get_section_size(x) ((x)->_cooked_size)
146 #endif
148 struct label_map {
149 asymbol *csym;
150 const char *orig_label;
151 const char *label;
152 int count;
153 int index;
155 DECLARE_VEC_TYPE(struct label_map, label_map_vec);
157 struct superbfd {
158 bfd *abfd;
159 struct asymbolp_vec syms;
160 struct supersect *new_supersects;
161 struct label_map_vec maps;
164 struct supersect {
165 struct superbfd *parent;
166 const char *name;
167 flagword flags;
168 struct void_vec contents;
169 int alignment;
170 struct arelentp_vec relocs;
171 struct arelentp_vec new_relocs;
172 struct supersect *next;
173 asymbol *symbol;
176 struct kernel_symbol {
177 unsigned long value;
178 char *name;
181 struct superbfd *fetch_superbfd(bfd *abfd);
182 struct supersect *fetch_supersect(struct superbfd *sbfd, asection *sect);
183 struct supersect *new_supersect(struct superbfd *sbfd, const char *name);
184 void supersect_move(struct supersect *dest_ss, struct supersect *src_ss);
186 #define sect_grow(ss, n, type) \
187 ((type *)sect_do_grow(ss, n, sizeof(type), __alignof__(type)))
188 void *sect_do_grow(struct supersect *ss, size_t n, size_t size, int alignment);
190 #define sect_copy(dest_ss, dest, src_ss, src, n) \
191 sect_do_copy(dest_ss, dest, src_ss, src, (n) * sizeof(*(src)))
192 void sect_do_copy(struct supersect *dest_ss, void *dest,
193 struct supersect *src_ss, const void *src, size_t n);
195 #define starts_with(str, prefix) \
196 (strncmp(str, prefix, strlen(prefix)) == 0)
197 #define ends_with(str, suffix) \
198 (strlen(str) >= strlen(suffix) && \
199 strcmp(&str[strlen(str) - strlen(suffix)], suffix) == 0)
201 bfd_vma addr_offset(struct supersect *ss, const void *addr);
202 bfd_vma get_reloc_offset(struct supersect *ss, arelent *reloc, int adjust_pc);
203 bfd_vma read_reloc(struct supersect *ss, const void *addr, size_t size,
204 asymbol **symp);
205 const void *read_pointer(struct supersect *ss, void *const *addr,
206 struct supersect **ssp);
207 const char *read_string(struct supersect *ss, const char *const *addr);
208 char *str_pointer(struct supersect *ss, void *const *addr);
210 #define read_num(ss, addr) ((typeof(*(addr))) \
211 read_reloc(ss, addr, sizeof(*(addr)), NULL))
213 asymbol *canonical_symbol(struct superbfd *sbfd, asymbol *sym);
214 asymbol **canonical_symbolp(struct superbfd *sbfd, asymbol *sym);
215 const char *label_lookup(struct superbfd *sbfd, asymbol *sym);