1 | /* |
---|
2 | * COPYRIGHT (c) 2012, 2018 Chris Johns <chrisj@rtems.org> |
---|
3 | * |
---|
4 | * The license and distribution terms for this file may be |
---|
5 | * found in the file LICENSE in this distribution or at |
---|
6 | * http://www.rtems.org/license/LICENSE. |
---|
7 | */ |
---|
8 | /** |
---|
9 | * @file |
---|
10 | * |
---|
11 | * @ingroup rtl |
---|
12 | * |
---|
13 | * @brief RTEMS Run-Time Linker Error |
---|
14 | */ |
---|
15 | |
---|
16 | #if HAVE_CONFIG_H |
---|
17 | #include "config.h" |
---|
18 | #endif |
---|
19 | |
---|
20 | #include <errno.h> |
---|
21 | #include <inttypes.h> |
---|
22 | #include <stdlib.h> |
---|
23 | #include <stdio.h> |
---|
24 | #include <string.h> |
---|
25 | |
---|
26 | #include <rtems/libio_.h> |
---|
27 | |
---|
28 | #include <rtems/rtl/rtl.h> |
---|
29 | #include "rtl-chain-iterator.h" |
---|
30 | #include <rtems/rtl/rtl-obj.h> |
---|
31 | #include "rtl-error.h" |
---|
32 | #include "rtl-find-file.h" |
---|
33 | #include "rtl-string.h" |
---|
34 | #include <rtems/rtl/rtl-trace.h> |
---|
35 | |
---|
36 | #define RTEMS_RTL_ELF_LOADER 1 |
---|
37 | #define RTEMS_RTL_RAP_LOADER 1 |
---|
38 | |
---|
39 | #if RTEMS_RTL_RAP_LOADER |
---|
40 | #include "rtl-rap.h" |
---|
41 | #define RTEMS_RTL_RAP_LOADER_COUNT 1 |
---|
42 | #else |
---|
43 | #define RTEMS_RTL_RAP_LOADER_COUNT 0 |
---|
44 | #endif |
---|
45 | |
---|
46 | #if RTEMS_RTL_ELF_LOADER |
---|
47 | #include "rtl-elf.h" |
---|
48 | #define RTEMS_RTL_ELF_LOADER_COUNT 1 |
---|
49 | #else |
---|
50 | #define RTEMS_RTL_ELF_LOADER_COUNT 0 |
---|
51 | #endif |
---|
52 | |
---|
53 | /** |
---|
54 | * The table of supported loader formats. |
---|
55 | */ |
---|
56 | #define RTEMS_RTL_LOADERS (RTEMS_RTL_ELF_LOADER_COUNT + RTEMS_RTL_RAP_LOADER_COUNT) |
---|
57 | static const rtems_rtl_loader_table loaders[RTEMS_RTL_LOADERS] = |
---|
58 | { |
---|
59 | #if RTEMS_RTL_RAP_LOADER |
---|
60 | { .check = rtems_rtl_rap_file_check, |
---|
61 | .load = rtems_rtl_rap_file_load, |
---|
62 | .unload = rtems_rtl_rap_file_unload, |
---|
63 | .unload = rtems_rtl_rap_file_unload, |
---|
64 | .signature = rtems_rtl_rap_file_sig }, |
---|
65 | #endif |
---|
66 | #if RTEMS_RTL_ELF_LOADER |
---|
67 | { .check = rtems_rtl_elf_file_check, |
---|
68 | .load = rtems_rtl_elf_file_load, |
---|
69 | .unload = rtems_rtl_elf_file_unload, |
---|
70 | .signature = rtems_rtl_elf_file_sig }, |
---|
71 | #endif |
---|
72 | }; |
---|
73 | |
---|
74 | rtems_rtl_obj* |
---|
75 | rtems_rtl_obj_alloc (void) |
---|
76 | { |
---|
77 | rtems_rtl_obj* obj = rtems_rtl_alloc_new (RTEMS_RTL_ALLOC_OBJECT, |
---|
78 | sizeof (rtems_rtl_obj), |
---|
79 | true); |
---|
80 | if (obj) |
---|
81 | { |
---|
82 | /* |
---|
83 | * Initialise the chains. |
---|
84 | */ |
---|
85 | rtems_chain_initialize_empty (&obj->sections); |
---|
86 | rtems_chain_initialize_empty (&obj->dependents); |
---|
87 | /* |
---|
88 | * No valid format. |
---|
89 | */ |
---|
90 | obj->format = -1; |
---|
91 | } |
---|
92 | return obj; |
---|
93 | } |
---|
94 | |
---|
95 | static void |
---|
96 | rtems_rtl_obj_free_names (rtems_rtl_obj* obj) |
---|
97 | { |
---|
98 | if (rtems_rtl_obj_oname_valid (obj)) |
---|
99 | rtems_rtl_alloc_del (RTEMS_RTL_ALLOC_OBJECT, (void*) obj->oname); |
---|
100 | if (rtems_rtl_obj_aname_valid (obj)) |
---|
101 | rtems_rtl_alloc_del (RTEMS_RTL_ALLOC_OBJECT, (void*) obj->aname); |
---|
102 | if (rtems_rtl_obj_fname_valid (obj)) |
---|
103 | rtems_rtl_alloc_del (RTEMS_RTL_ALLOC_OBJECT, (void*) obj->fname); |
---|
104 | } |
---|
105 | |
---|
106 | bool |
---|
107 | rtems_rtl_obj_free (rtems_rtl_obj* obj) |
---|
108 | { |
---|
109 | if (obj->users > 0 || ((obj->flags & RTEMS_RTL_OBJ_LOCKED) != 0)) |
---|
110 | { |
---|
111 | rtems_rtl_set_error (EINVAL, "cannot free obj still in use"); |
---|
112 | return false; |
---|
113 | } |
---|
114 | if (!rtems_chain_is_node_off_chain (&obj->link)) |
---|
115 | rtems_chain_extract (&obj->link); |
---|
116 | rtems_rtl_alloc_module_del (&obj->text_base, &obj->const_base, &obj->eh_base, |
---|
117 | &obj->data_base, &obj->bss_base); |
---|
118 | rtems_rtl_obj_erase_sections (obj); |
---|
119 | rtems_rtl_obj_erase_dependents (obj); |
---|
120 | rtems_rtl_symbol_obj_erase (obj); |
---|
121 | rtems_rtl_obj_erase_trampoline (obj); |
---|
122 | rtems_rtl_obj_free_names (obj); |
---|
123 | if (obj->sec_num != NULL) |
---|
124 | free (obj->sec_num); |
---|
125 | if (obj->linkmap != NULL) |
---|
126 | rtems_rtl_alloc_del (RTEMS_RTL_ALLOC_OBJECT, (void*) obj->linkmap); |
---|
127 | rtems_rtl_alloc_del (RTEMS_RTL_ALLOC_OBJECT, obj); |
---|
128 | return true; |
---|
129 | } |
---|
130 | |
---|
131 | typedef struct rtems_rtl_obj_unresolved_data |
---|
132 | { |
---|
133 | bool has_unresolved; |
---|
134 | } rtems_rtl_obj_unresolved_data; |
---|
135 | |
---|
136 | static bool |
---|
137 | rtems_rtl_obj_unresolved_dependent (rtems_rtl_obj* obj, |
---|
138 | rtems_rtl_obj* dependent, |
---|
139 | void* data) |
---|
140 | { |
---|
141 | rtems_rtl_obj_unresolved_data* ud; |
---|
142 | ud = (rtems_rtl_obj_unresolved_data*) data; |
---|
143 | if ((dependent->flags & RTEMS_RTL_OBJ_DEP_VISITED) == 0) |
---|
144 | { |
---|
145 | dependent->flags |= RTEMS_RTL_OBJ_DEP_VISITED; |
---|
146 | if ((dependent->flags & RTEMS_RTL_OBJ_UNRESOLVED) != 0) |
---|
147 | ud->has_unresolved = true; |
---|
148 | else |
---|
149 | { |
---|
150 | rtems_rtl_obj_iterate_dependents (dependent, |
---|
151 | rtems_rtl_obj_unresolved_dependent, |
---|
152 | ud); |
---|
153 | } |
---|
154 | if (rtems_rtl_trace (RTEMS_RTL_TRACE_UNRESOLVED)) |
---|
155 | printf ("rtl: obj: unresolved: dep: %s is %s\n", |
---|
156 | dependent->oname, ud->has_unresolved ? "unresolved" : "resolved"); |
---|
157 | } |
---|
158 | return ud->has_unresolved; |
---|
159 | } |
---|
160 | |
---|
161 | static bool |
---|
162 | rtems_rtl_obj_unresolved_object (rtems_chain_node* node, void* data) |
---|
163 | { |
---|
164 | rtems_rtl_obj* obj = (rtems_rtl_obj*) node; |
---|
165 | rtems_rtl_obj_unresolved_data* ud; |
---|
166 | ud = (rtems_rtl_obj_unresolved_data*) data; |
---|
167 | ud->has_unresolved = (obj->flags & RTEMS_RTL_OBJ_UNRESOLVED) != 0; |
---|
168 | return !ud->has_unresolved; |
---|
169 | } |
---|
170 | |
---|
171 | bool |
---|
172 | rtems_rtl_obj_unresolved (rtems_rtl_obj* obj) |
---|
173 | { |
---|
174 | rtems_rtl_obj_unresolved_data ud = { |
---|
175 | .has_unresolved = (obj->flags & RTEMS_RTL_OBJ_UNRESOLVED) != 0 |
---|
176 | }; |
---|
177 | if (rtems_rtl_trace (RTEMS_RTL_TRACE_UNRESOLVED)) |
---|
178 | printf ("rtl: obj: unresolved: dep: %s is %s\n", |
---|
179 | obj->oname, ud.has_unresolved ? "unresolved" : "resolved"); |
---|
180 | if (!ud.has_unresolved) |
---|
181 | { |
---|
182 | if ((obj->flags & RTEMS_RTL_OBJ_BASE) != 0) |
---|
183 | { |
---|
184 | rtems_rtl_data* rtl = rtems_rtl_data_unprotected (); |
---|
185 | rtems_rtl_chain_iterate (&rtl->objects, |
---|
186 | rtems_rtl_obj_unresolved_object, |
---|
187 | &ud); |
---|
188 | } |
---|
189 | else |
---|
190 | { |
---|
191 | rtems_rtl_obj_update_flags (RTEMS_RTL_OBJ_DEP_VISITED, 0); |
---|
192 | obj->flags |= RTEMS_RTL_OBJ_DEP_VISITED; |
---|
193 | rtems_rtl_obj_iterate_dependents (obj, |
---|
194 | rtems_rtl_obj_unresolved_dependent, |
---|
195 | &ud); |
---|
196 | rtems_rtl_obj_update_flags (RTEMS_RTL_OBJ_DEP_VISITED, 0); |
---|
197 | } |
---|
198 | } |
---|
199 | return ud.has_unresolved; |
---|
200 | } |
---|
201 | |
---|
202 | bool |
---|
203 | rtems_rtl_parse_name (const char* name, |
---|
204 | const char** aname, |
---|
205 | const char** oname, |
---|
206 | off_t* ooffset) |
---|
207 | { |
---|
208 | const char* laname = NULL; |
---|
209 | const char* loname = NULL; |
---|
210 | const char* colon; |
---|
211 | const char* end; |
---|
212 | |
---|
213 | /* |
---|
214 | * Parse the name to determine if the object file is part of an archive or it |
---|
215 | * is an object file. If an archive check the name for a '@' to see if the |
---|
216 | * archive contains an offset. |
---|
217 | * |
---|
218 | * Note, if an archive the object file oofset may be know but the |
---|
219 | * object file is not. Leave the object name as a NULL. |
---|
220 | */ |
---|
221 | end = name + strlen (name); |
---|
222 | colon = strrchr (name, ':'); |
---|
223 | if (colon == NULL || colon < strrchr(name, '/')) |
---|
224 | colon = end; |
---|
225 | |
---|
226 | loname = rtems_rtl_alloc_new (RTEMS_RTL_ALLOC_OBJECT, colon - name + 1, true); |
---|
227 | if (!loname) |
---|
228 | { |
---|
229 | rtems_rtl_set_error (ENOMEM, "no memory for object file name"); |
---|
230 | return false; |
---|
231 | } |
---|
232 | |
---|
233 | memcpy ((void*) loname, name, colon - name); |
---|
234 | |
---|
235 | /* |
---|
236 | * If the pointers match there is no ':' delimiter. |
---|
237 | */ |
---|
238 | if (colon != end) |
---|
239 | { |
---|
240 | const char* at; |
---|
241 | |
---|
242 | /* |
---|
243 | * The file name is an archive and the object file name is next after the |
---|
244 | * delimiter. Move the pointer to the archive name. |
---|
245 | */ |
---|
246 | laname = loname; |
---|
247 | ++colon; |
---|
248 | |
---|
249 | /* |
---|
250 | * See if there is a '@' to delimit an archive offset for the object in the |
---|
251 | * archive. |
---|
252 | */ |
---|
253 | at = strchr (colon, '@'); |
---|
254 | |
---|
255 | if (at == NULL) |
---|
256 | at = end; |
---|
257 | |
---|
258 | |
---|
259 | loname = rtems_rtl_alloc_new (RTEMS_RTL_ALLOC_OBJECT, at - colon + 1, true); |
---|
260 | if (!loname) |
---|
261 | { |
---|
262 | rtems_rtl_alloc_del (RTEMS_RTL_ALLOC_OBJECT, (void*) laname); |
---|
263 | rtems_rtl_set_error (ENOMEM, "no memory for object file name"); |
---|
264 | return false; |
---|
265 | } |
---|
266 | |
---|
267 | memcpy ((void*) loname, colon, at - colon); |
---|
268 | |
---|
269 | if (at != end) |
---|
270 | { |
---|
271 | /* |
---|
272 | * The object name has an archive offset. If the number |
---|
273 | * does not parse 0 will be returned and the archive will be |
---|
274 | * searched. |
---|
275 | */ |
---|
276 | *ooffset = strtoul (at + 1, 0, 0); |
---|
277 | } |
---|
278 | } |
---|
279 | |
---|
280 | *oname = loname; |
---|
281 | *aname = laname; |
---|
282 | return true; |
---|
283 | } |
---|
284 | |
---|
285 | static bool |
---|
286 | rtems_rtl_obj_parse_name (rtems_rtl_obj* obj, const char* name) |
---|
287 | { |
---|
288 | return rtems_rtl_parse_name (name, &(obj->aname), &(obj->oname), &(obj->ooffset)); |
---|
289 | } |
---|
290 | |
---|
291 | /** |
---|
292 | * Section size summer iterator data. |
---|
293 | */ |
---|
294 | typedef struct |
---|
295 | { |
---|
296 | uint32_t mask; /**< The selection mask to sum. */ |
---|
297 | size_t size; /**< The size of all section fragments. */ |
---|
298 | } rtems_rtl_obj_sect_summer_data; |
---|
299 | |
---|
300 | static bool |
---|
301 | rtems_rtl_obj_sect_summer (rtems_chain_node* node, void* data) |
---|
302 | { |
---|
303 | rtems_rtl_obj_sect* sect = (rtems_rtl_obj_sect*) node; |
---|
304 | if ((sect->flags & RTEMS_RTL_OBJ_SECT_ARCH_ALLOC) == 0) |
---|
305 | { |
---|
306 | rtems_rtl_obj_sect_summer_data* summer = data; |
---|
307 | if ((sect->flags & summer->mask) == summer->mask) |
---|
308 | summer->size = |
---|
309 | rtems_rtl_obj_align (summer->size, sect->alignment) + sect->size; |
---|
310 | } |
---|
311 | return true; |
---|
312 | } |
---|
313 | |
---|
314 | static size_t |
---|
315 | rtems_rtl_obj_section_size (const rtems_rtl_obj* obj, uint32_t mask) |
---|
316 | { |
---|
317 | rtems_rtl_obj_sect_summer_data summer; |
---|
318 | summer.mask = mask; |
---|
319 | summer.size = 0; |
---|
320 | rtems_rtl_chain_iterate ((rtems_chain_control*) &obj->sections, |
---|
321 | rtems_rtl_obj_sect_summer, |
---|
322 | &summer); |
---|
323 | return summer.size; |
---|
324 | } |
---|
325 | |
---|
326 | /** |
---|
327 | * Section alignment iterator data. The first section's alignment sets the |
---|
328 | * alignment for that type of section. |
---|
329 | */ |
---|
330 | typedef struct |
---|
331 | { |
---|
332 | uint32_t mask; /**< The selection mask to look for alignment. */ |
---|
333 | uint32_t alignment; /**< The alignment of the section type. */ |
---|
334 | } rtems_rtl_obj_sect_aligner_data; |
---|
335 | |
---|
336 | /** |
---|
337 | * The section aligner iterator. |
---|
338 | */ |
---|
339 | static bool |
---|
340 | rtems_rtl_obj_sect_aligner (rtems_chain_node* node, void* data) |
---|
341 | { |
---|
342 | rtems_rtl_obj_sect* sect = (rtems_rtl_obj_sect*) node; |
---|
343 | rtems_rtl_obj_sect_aligner_data* aligner = data; |
---|
344 | if ((sect->flags & aligner->mask) == aligner->mask) |
---|
345 | { |
---|
346 | aligner->alignment = sect->alignment; |
---|
347 | return false; |
---|
348 | } |
---|
349 | return true; |
---|
350 | } |
---|
351 | |
---|
352 | static size_t |
---|
353 | rtems_rtl_obj_section_alignment (const rtems_rtl_obj* obj, uint32_t mask) |
---|
354 | { |
---|
355 | rtems_rtl_obj_sect_aligner_data aligner; |
---|
356 | aligner.mask = mask; |
---|
357 | aligner.alignment = 0; |
---|
358 | rtems_rtl_chain_iterate ((rtems_chain_control*) &obj->sections, |
---|
359 | rtems_rtl_obj_sect_aligner, |
---|
360 | &aligner); |
---|
361 | return aligner.alignment; |
---|
362 | } |
---|
363 | |
---|
364 | static bool |
---|
365 | rtems_rtl_obj_section_handler (uint32_t mask, |
---|
366 | rtems_rtl_obj* obj, |
---|
367 | int fd, |
---|
368 | rtems_rtl_obj_sect_handler handler, |
---|
369 | void* data) |
---|
370 | { |
---|
371 | rtems_chain_node* node = rtems_chain_first (&obj->sections); |
---|
372 | while (!rtems_chain_is_tail (&obj->sections, node)) |
---|
373 | { |
---|
374 | rtems_rtl_obj_sect* sect = (rtems_rtl_obj_sect*) node; |
---|
375 | if ((sect->flags & mask) != 0) |
---|
376 | { |
---|
377 | if (!handler (obj, fd, sect, data)) |
---|
378 | return false; |
---|
379 | } |
---|
380 | node = rtems_chain_next (node); |
---|
381 | } |
---|
382 | return true; |
---|
383 | } |
---|
384 | |
---|
385 | bool |
---|
386 | rtems_rtl_obj_find_file (rtems_rtl_obj* obj, const char* name) |
---|
387 | { |
---|
388 | const char* pname; |
---|
389 | rtems_rtl_data* rtl; |
---|
390 | |
---|
391 | /* |
---|
392 | * Parse the name. The object descriptor will have the archive name and/or |
---|
393 | * object name fields filled in. A find of the file will result in the file |
---|
394 | * name (fname) field pointing to the actual file if present on the file |
---|
395 | * system. |
---|
396 | */ |
---|
397 | if (!rtems_rtl_obj_parse_name (obj, name)) |
---|
398 | return false; |
---|
399 | |
---|
400 | /* |
---|
401 | * If the archive field (aname) is set we use that name else we use the |
---|
402 | * object field (oname). If selected name is absolute we just point the aname |
---|
403 | * field to the fname field to that name. If the field is relative we search |
---|
404 | * the paths set in the RTL for the file. |
---|
405 | */ |
---|
406 | if (rtems_rtl_obj_aname_valid (obj)) |
---|
407 | pname = rtems_rtl_obj_aname (obj); |
---|
408 | else |
---|
409 | pname = rtems_rtl_obj_oname (obj); |
---|
410 | |
---|
411 | rtl = rtems_rtl_lock (); |
---|
412 | |
---|
413 | if (!rtems_rtl_find_file (pname, rtl->paths, &obj->fname, &obj->fsize)) |
---|
414 | { |
---|
415 | rtems_rtl_set_error (ENOENT, "file not found"); |
---|
416 | rtems_rtl_unlock (); |
---|
417 | return false; |
---|
418 | } |
---|
419 | |
---|
420 | rtems_rtl_unlock (); |
---|
421 | |
---|
422 | return true; |
---|
423 | } |
---|
424 | |
---|
425 | bool |
---|
426 | rtems_rtl_obj_add_section (rtems_rtl_obj* obj, |
---|
427 | int section, |
---|
428 | const char* name, |
---|
429 | size_t size, |
---|
430 | off_t offset, |
---|
431 | uint32_t alignment, |
---|
432 | int link, |
---|
433 | int info, |
---|
434 | uint32_t flags) |
---|
435 | { |
---|
436 | if (size > 0) |
---|
437 | { |
---|
438 | rtems_rtl_obj_sect* sect = rtems_rtl_alloc_new (RTEMS_RTL_ALLOC_OBJECT, |
---|
439 | sizeof (rtems_rtl_obj_sect), |
---|
440 | true); |
---|
441 | if (!sect) |
---|
442 | { |
---|
443 | rtems_rtl_set_error (ENOMEM, "adding allocated section"); |
---|
444 | return false; |
---|
445 | } |
---|
446 | sect->section = section; |
---|
447 | sect->name = rtems_rtl_strdup (name); |
---|
448 | sect->size = size; |
---|
449 | sect->offset = offset; |
---|
450 | sect->alignment = alignment; |
---|
451 | sect->link = link; |
---|
452 | sect->info = info; |
---|
453 | sect->flags = flags; |
---|
454 | sect->base = NULL; |
---|
455 | rtems_chain_append (&obj->sections, §->node); |
---|
456 | |
---|
457 | if (rtems_rtl_trace (RTEMS_RTL_TRACE_SECTION)) |
---|
458 | printf ("rtl: sect: add: %-2d: %s (%zu) 0x%08" PRIu32 "\n", |
---|
459 | section, name, size, flags); |
---|
460 | } |
---|
461 | return true; |
---|
462 | } |
---|
463 | |
---|
464 | void |
---|
465 | rtems_rtl_obj_erase_sections (rtems_rtl_obj* obj) |
---|
466 | { |
---|
467 | rtems_chain_node* node = rtems_chain_first (&obj->sections); |
---|
468 | while (!rtems_chain_is_tail (&obj->sections, node)) |
---|
469 | { |
---|
470 | rtems_rtl_obj_sect* sect = (rtems_rtl_obj_sect*) node; |
---|
471 | rtems_chain_node* next_node = rtems_chain_next (node); |
---|
472 | rtems_chain_extract (node); |
---|
473 | rtems_rtl_alloc_del (RTEMS_RTL_ALLOC_OBJECT, (void*) sect->name); |
---|
474 | rtems_rtl_alloc_del (RTEMS_RTL_ALLOC_OBJECT, sect); |
---|
475 | node = next_node; |
---|
476 | } |
---|
477 | } |
---|
478 | |
---|
479 | /** |
---|
480 | * Section finder iterator data. |
---|
481 | */ |
---|
482 | typedef struct |
---|
483 | { |
---|
484 | rtems_rtl_obj_sect* sect; /**< The matching section. */ |
---|
485 | const char* name; /**< The name to match. */ |
---|
486 | int index; /**< The index to match. */ |
---|
487 | uint32_t mask; /**< The mask to match. */ |
---|
488 | unsigned int flags; /**< The flags to use when matching. */ |
---|
489 | } rtems_rtl_obj_sect_finder; |
---|
490 | |
---|
491 | static bool |
---|
492 | rtems_rtl_obj_sect_match_name (rtems_chain_node* node, void* data) |
---|
493 | { |
---|
494 | rtems_rtl_obj_sect* sect = (rtems_rtl_obj_sect*) node; |
---|
495 | rtems_rtl_obj_sect_finder* match = data; |
---|
496 | if (strcmp (sect->name, match->name) == 0) |
---|
497 | { |
---|
498 | match->sect = sect; |
---|
499 | return false; |
---|
500 | } |
---|
501 | return true; |
---|
502 | } |
---|
503 | |
---|
504 | rtems_rtl_obj_sect* |
---|
505 | rtems_rtl_obj_find_section (const rtems_rtl_obj* obj, |
---|
506 | const char* name) |
---|
507 | { |
---|
508 | rtems_rtl_obj_sect_finder match; |
---|
509 | match.sect = NULL; |
---|
510 | match.name = name; |
---|
511 | match.mask = 0; |
---|
512 | match.flags = 0; |
---|
513 | rtems_rtl_chain_iterate ((rtems_chain_control*) &obj->sections, |
---|
514 | rtems_rtl_obj_sect_match_name, |
---|
515 | &match); |
---|
516 | return match.sect; |
---|
517 | } |
---|
518 | |
---|
519 | static bool |
---|
520 | rtems_rtl_obj_sect_match_index (rtems_chain_node* node, void* data) |
---|
521 | { |
---|
522 | rtems_rtl_obj_sect* sect = (rtems_rtl_obj_sect*) node; |
---|
523 | rtems_rtl_obj_sect_finder* match = data; |
---|
524 | if (sect->section == match->index) |
---|
525 | { |
---|
526 | match->sect = sect; |
---|
527 | return false; |
---|
528 | } |
---|
529 | return true; |
---|
530 | } |
---|
531 | |
---|
532 | rtems_rtl_obj_sect* |
---|
533 | rtems_rtl_obj_find_section_by_index (const rtems_rtl_obj* obj, |
---|
534 | int index) |
---|
535 | { |
---|
536 | rtems_rtl_obj_sect_finder match; |
---|
537 | match.sect = NULL; |
---|
538 | match.index = index; |
---|
539 | match.mask = 0; |
---|
540 | match.flags = 0; |
---|
541 | rtems_rtl_chain_iterate ((rtems_chain_control*) &obj->sections, |
---|
542 | rtems_rtl_obj_sect_match_index, |
---|
543 | &match); |
---|
544 | return match.sect; |
---|
545 | } |
---|
546 | |
---|
547 | static bool |
---|
548 | rtems_rtl_obj_sect_match_mask (rtems_chain_node* node, void* data) |
---|
549 | { |
---|
550 | rtems_rtl_obj_sect* sect = (rtems_rtl_obj_sect*) node; |
---|
551 | rtems_rtl_obj_sect_finder* match = data; |
---|
552 | if (match->flags == 0) |
---|
553 | { |
---|
554 | if (match->index < 0 || sect->section == match->index) |
---|
555 | match->flags = 1; |
---|
556 | if (match->index >= 0) |
---|
557 | return true; |
---|
558 | } |
---|
559 | if ((sect->flags & match->mask) != 0) |
---|
560 | { |
---|
561 | match->sect = sect; |
---|
562 | return false; |
---|
563 | } |
---|
564 | return true; |
---|
565 | } |
---|
566 | |
---|
567 | rtems_rtl_obj_sect* |
---|
568 | rtems_rtl_obj_find_section_by_mask (const rtems_rtl_obj* obj, |
---|
569 | int index, |
---|
570 | uint32_t mask) |
---|
571 | { |
---|
572 | rtems_rtl_obj_sect_finder match; |
---|
573 | match.sect = NULL; |
---|
574 | match.index = index; |
---|
575 | match.mask = mask; |
---|
576 | match.flags = 0; |
---|
577 | rtems_rtl_chain_iterate ((rtems_chain_control*) &obj->sections, |
---|
578 | rtems_rtl_obj_sect_match_mask, |
---|
579 | &match); |
---|
580 | return match.sect; |
---|
581 | } |
---|
582 | |
---|
583 | bool |
---|
584 | rtems_rtl_obj_alloc_trampoline (rtems_rtl_obj* obj) |
---|
585 | { |
---|
586 | if (obj->tramp_size == 0) |
---|
587 | return true; |
---|
588 | obj->trampoline = rtems_rtl_alloc_new (RTEMS_RTL_ALLOC_OBJECT, |
---|
589 | obj->tramp_size, |
---|
590 | true); |
---|
591 | if (obj->trampoline == NULL) |
---|
592 | rtems_rtl_set_error (ENOMEM, "no memory for the trampoline"); |
---|
593 | obj->tramp_brk = obj->trampoline; |
---|
594 | return obj->trampoline != NULL; |
---|
595 | } |
---|
596 | |
---|
597 | void |
---|
598 | rtems_rtl_obj_erase_trampoline (rtems_rtl_obj* obj) |
---|
599 | { |
---|
600 | rtems_rtl_alloc_del (RTEMS_RTL_ALLOC_OBJECT, obj->trampoline); |
---|
601 | } |
---|
602 | |
---|
603 | bool |
---|
604 | rtems_rtl_obj_alloc_dependents (rtems_rtl_obj* obj, size_t dependents) |
---|
605 | { |
---|
606 | rtems_rtl_obj_depends* depends; |
---|
607 | size_t size; |
---|
608 | |
---|
609 | size = sizeof (rtems_rtl_obj_depends) + sizeof (rtems_rtl_obj*) * dependents; |
---|
610 | |
---|
611 | depends = rtems_rtl_alloc_new (RTEMS_RTL_ALLOC_OBJECT, |
---|
612 | size, |
---|
613 | true); |
---|
614 | if (depends == NULL) |
---|
615 | { |
---|
616 | rtems_rtl_set_error (ENOMEM, "no memory for the dependency"); |
---|
617 | } |
---|
618 | else |
---|
619 | { |
---|
620 | depends->dependents = dependents; |
---|
621 | rtems_chain_append (&obj->dependents, &depends->node); |
---|
622 | } |
---|
623 | |
---|
624 | return depends != NULL; |
---|
625 | } |
---|
626 | |
---|
627 | void |
---|
628 | rtems_rtl_obj_erase_dependents (rtems_rtl_obj* obj) |
---|
629 | { |
---|
630 | rtems_chain_node* node = rtems_chain_first (&obj->dependents); |
---|
631 | while (!rtems_chain_is_tail (&obj->dependents, node)) |
---|
632 | { |
---|
633 | rtems_rtl_obj_depends* depends = (rtems_rtl_obj_depends*) node; |
---|
634 | rtems_chain_node* next_node = rtems_chain_next (node); |
---|
635 | rtems_chain_extract (node); |
---|
636 | rtems_rtl_alloc_del (RTEMS_RTL_ALLOC_OBJECT, depends); |
---|
637 | node = next_node; |
---|
638 | } |
---|
639 | } |
---|
640 | |
---|
641 | bool |
---|
642 | rtems_rtl_obj_add_dependent (rtems_rtl_obj* obj, rtems_rtl_obj* dependent) |
---|
643 | { |
---|
644 | rtems_rtl_obj** free_slot; |
---|
645 | rtems_chain_node* node; |
---|
646 | |
---|
647 | if (obj == dependent || dependent == rtems_rtl_baseimage ()) |
---|
648 | return false; |
---|
649 | |
---|
650 | if (rtems_rtl_trace (RTEMS_RTL_TRACE_DEPENDENCY)) |
---|
651 | printf ("rtl: depend: add: %s -> %s\n", obj->oname, dependent->oname); |
---|
652 | |
---|
653 | free_slot = NULL; |
---|
654 | |
---|
655 | node = rtems_chain_first (&obj->dependents); |
---|
656 | while (!rtems_chain_is_tail (&obj->dependents, node)) |
---|
657 | { |
---|
658 | rtems_rtl_obj_depends* depends = (rtems_rtl_obj_depends*) node; |
---|
659 | size_t d; |
---|
660 | for (d = 0; d < depends->dependents; ++d) |
---|
661 | { |
---|
662 | if (free_slot == NULL && depends->depends[d] == NULL) |
---|
663 | free_slot = &(depends->depends[d]); |
---|
664 | if (depends->depends[d] == dependent) |
---|
665 | return false; |
---|
666 | } |
---|
667 | node = rtems_chain_next (node); |
---|
668 | } |
---|
669 | |
---|
670 | if (free_slot == NULL) |
---|
671 | { |
---|
672 | if (rtems_rtl_obj_alloc_dependents (obj, |
---|
673 | RTEMS_RTL_DEPENDENCY_BLOCK_SIZE)) |
---|
674 | { |
---|
675 | rtems_rtl_obj_depends* depends; |
---|
676 | node = rtems_chain_last (&obj->dependents); |
---|
677 | depends = (rtems_rtl_obj_depends*) node; |
---|
678 | free_slot = &(depends->depends[0]); |
---|
679 | if (*free_slot != NULL) |
---|
680 | { |
---|
681 | rtems_rtl_set_error (EINVAL, "new dependency node not empty"); |
---|
682 | free_slot = NULL; |
---|
683 | } |
---|
684 | } |
---|
685 | } |
---|
686 | |
---|
687 | if (free_slot != NULL) |
---|
688 | *free_slot = dependent; |
---|
689 | |
---|
690 | return free_slot != NULL; |
---|
691 | } |
---|
692 | |
---|
693 | |
---|
694 | bool |
---|
695 | rtems_rtl_obj_remove_dependencies (rtems_rtl_obj* obj) |
---|
696 | { |
---|
697 | /* |
---|
698 | * If there are no references unload the object. |
---|
699 | */ |
---|
700 | if (obj->refs == 0) |
---|
701 | { |
---|
702 | /* |
---|
703 | * Remove the refences from the object files this file depend on. The |
---|
704 | * unload happens once the list of objects to be unloaded has been made and |
---|
705 | * the destructors have been called for all those modules. |
---|
706 | */ |
---|
707 | rtems_chain_node* node = rtems_chain_first (&obj->dependents); |
---|
708 | while (!rtems_chain_is_tail (&obj->dependents, node)) |
---|
709 | { |
---|
710 | rtems_rtl_obj_depends* depends = (rtems_rtl_obj_depends*) node; |
---|
711 | size_t d; |
---|
712 | for (d = 0; d < depends->dependents; ++d) |
---|
713 | { |
---|
714 | if (depends->depends[d] != NULL) |
---|
715 | { |
---|
716 | rtems_rtl_obj_dec_reference (depends->depends[d]); |
---|
717 | depends->depends[d] = NULL; |
---|
718 | } |
---|
719 | } |
---|
720 | node = rtems_chain_next (node); |
---|
721 | } |
---|
722 | return true; |
---|
723 | } |
---|
724 | return false; |
---|
725 | } |
---|
726 | |
---|
727 | bool |
---|
728 | rtems_rtl_obj_iterate_dependents (rtems_rtl_obj* obj, |
---|
729 | rtems_rtl_obj_depends_iterator iterator, |
---|
730 | void* data) |
---|
731 | { |
---|
732 | rtems_chain_node* node = rtems_chain_first (&obj->dependents); |
---|
733 | while (!rtems_chain_is_tail (&obj->dependents, node)) |
---|
734 | { |
---|
735 | rtems_rtl_obj_depends* depends = (rtems_rtl_obj_depends*) node; |
---|
736 | size_t d; |
---|
737 | for (d = 0; d < depends->dependents; ++d) |
---|
738 | { |
---|
739 | if (depends->depends[d]) |
---|
740 | { |
---|
741 | if (iterator (obj, depends->depends[d], data)) |
---|
742 | return true; |
---|
743 | } |
---|
744 | } |
---|
745 | node = rtems_chain_next (node); |
---|
746 | } |
---|
747 | return false; |
---|
748 | } |
---|
749 | |
---|
750 | size_t |
---|
751 | rtems_rtl_obj_text_size (const rtems_rtl_obj* obj) |
---|
752 | { |
---|
753 | const uint32_t flags = RTEMS_RTL_OBJ_SECT_LOAD | RTEMS_RTL_OBJ_SECT_TEXT; |
---|
754 | return rtems_rtl_obj_section_size (obj, flags); |
---|
755 | } |
---|
756 | |
---|
757 | uint32_t |
---|
758 | rtems_rtl_obj_text_alignment (const rtems_rtl_obj* obj) |
---|
759 | { |
---|
760 | const uint32_t flags = RTEMS_RTL_OBJ_SECT_LOAD | RTEMS_RTL_OBJ_SECT_TEXT; |
---|
761 | return rtems_rtl_obj_section_alignment (obj, flags); |
---|
762 | } |
---|
763 | |
---|
764 | size_t |
---|
765 | rtems_rtl_obj_const_size (const rtems_rtl_obj* obj) |
---|
766 | { |
---|
767 | const uint32_t flags = RTEMS_RTL_OBJ_SECT_LOAD | RTEMS_RTL_OBJ_SECT_CONST; |
---|
768 | return rtems_rtl_obj_section_size (obj, flags); |
---|
769 | } |
---|
770 | |
---|
771 | uint32_t |
---|
772 | rtems_rtl_obj_const_alignment (const rtems_rtl_obj* obj) |
---|
773 | { |
---|
774 | const uint32_t flags = RTEMS_RTL_OBJ_SECT_LOAD | RTEMS_RTL_OBJ_SECT_CONST; |
---|
775 | return rtems_rtl_obj_section_alignment (obj, flags); |
---|
776 | } |
---|
777 | |
---|
778 | uint32_t |
---|
779 | rtems_rtl_obj_eh_alignment (const rtems_rtl_obj* obj) |
---|
780 | { |
---|
781 | const uint32_t flags = RTEMS_RTL_OBJ_SECT_LOAD | RTEMS_RTL_OBJ_SECT_EH; |
---|
782 | return rtems_rtl_obj_section_alignment (obj, flags); |
---|
783 | } |
---|
784 | |
---|
785 | size_t |
---|
786 | rtems_rtl_obj_eh_size (const rtems_rtl_obj* obj) |
---|
787 | { |
---|
788 | const uint32_t flags = RTEMS_RTL_OBJ_SECT_LOAD | RTEMS_RTL_OBJ_SECT_EH; |
---|
789 | return rtems_rtl_obj_section_size (obj, flags); |
---|
790 | } |
---|
791 | |
---|
792 | size_t |
---|
793 | rtems_rtl_obj_data_size (const rtems_rtl_obj* obj) |
---|
794 | { |
---|
795 | const uint32_t flags = RTEMS_RTL_OBJ_SECT_LOAD | RTEMS_RTL_OBJ_SECT_DATA; |
---|
796 | return rtems_rtl_obj_section_size (obj, flags); |
---|
797 | } |
---|
798 | |
---|
799 | uint32_t |
---|
800 | rtems_rtl_obj_data_alignment (const rtems_rtl_obj* obj) |
---|
801 | { |
---|
802 | const uint32_t flags = RTEMS_RTL_OBJ_SECT_LOAD | RTEMS_RTL_OBJ_SECT_DATA; |
---|
803 | return rtems_rtl_obj_section_alignment (obj, flags); |
---|
804 | } |
---|
805 | |
---|
806 | size_t |
---|
807 | rtems_rtl_obj_bss_size (const rtems_rtl_obj* obj) |
---|
808 | { |
---|
809 | return rtems_rtl_obj_section_size (obj, RTEMS_RTL_OBJ_SECT_BSS); |
---|
810 | } |
---|
811 | |
---|
812 | uint32_t |
---|
813 | rtems_rtl_obj_bss_alignment (const rtems_rtl_obj* obj) |
---|
814 | { |
---|
815 | return rtems_rtl_obj_section_alignment (obj, RTEMS_RTL_OBJ_SECT_BSS); |
---|
816 | } |
---|
817 | |
---|
818 | bool |
---|
819 | rtems_rtl_obj_relocate (rtems_rtl_obj* obj, |
---|
820 | int fd, |
---|
821 | rtems_rtl_obj_sect_handler handler, |
---|
822 | void* data) |
---|
823 | { |
---|
824 | const uint32_t flags = (RTEMS_RTL_OBJ_SECT_LOAD | |
---|
825 | RTEMS_RTL_OBJ_SECT_REL | |
---|
826 | RTEMS_RTL_OBJ_SECT_RELA); |
---|
827 | bool r = rtems_rtl_obj_section_handler (flags, obj, fd, handler, data); |
---|
828 | rtems_rtl_obj_update_flags (RTEMS_RTL_OBJ_RELOC_TAG, 0); |
---|
829 | return r; |
---|
830 | } |
---|
831 | |
---|
832 | /** |
---|
833 | * Cache synchronization after runtime object load (dlopen) |
---|
834 | */ |
---|
835 | typedef struct |
---|
836 | { |
---|
837 | uint32_t mask; |
---|
838 | void *start_va; |
---|
839 | void *end_va; |
---|
840 | size_t cache_line_size; |
---|
841 | } rtems_rtl_obj_sect_sync_ctx; |
---|
842 | |
---|
843 | static bool |
---|
844 | rtems_rtl_obj_sect_sync_handler (rtems_chain_node* node, void* data) |
---|
845 | { |
---|
846 | rtems_rtl_obj_sect* sect = (rtems_rtl_obj_sect*) node; |
---|
847 | rtems_rtl_obj_sect_sync_ctx* sync_ctx = data; |
---|
848 | uintptr_t old_end; |
---|
849 | uintptr_t new_start; |
---|
850 | |
---|
851 | if ((sect->flags & sync_ctx->mask) == 0 || sect->size == 0) |
---|
852 | return true; |
---|
853 | |
---|
854 | if (sync_ctx->end_va == sync_ctx->start_va) |
---|
855 | { |
---|
856 | sync_ctx->start_va = sect->base; |
---|
857 | } |
---|
858 | else |
---|
859 | { |
---|
860 | old_end = (uintptr_t) sync_ctx->end_va & ~(sync_ctx->cache_line_size - 1); |
---|
861 | new_start = (uintptr_t) sect->base & ~(sync_ctx->cache_line_size - 1); |
---|
862 | if ((sect->base < sync_ctx->start_va) || |
---|
863 | (new_start - old_end > sync_ctx->cache_line_size)) |
---|
864 | { |
---|
865 | rtems_cache_instruction_sync_after_code_change(sync_ctx->start_va, |
---|
866 | sync_ctx->end_va - sync_ctx->start_va + 1); |
---|
867 | sync_ctx->start_va = sect->base; |
---|
868 | } |
---|
869 | } |
---|
870 | |
---|
871 | sync_ctx->end_va = sect->base + sect->size; |
---|
872 | |
---|
873 | return true; |
---|
874 | } |
---|
875 | |
---|
876 | void |
---|
877 | rtems_rtl_obj_synchronize_cache (rtems_rtl_obj* obj) |
---|
878 | { |
---|
879 | rtems_rtl_obj_sect_sync_ctx sync_ctx; |
---|
880 | |
---|
881 | if (rtems_cache_get_instruction_line_size() == 0) |
---|
882 | return; |
---|
883 | |
---|
884 | sync_ctx.cache_line_size = rtems_cache_get_maximal_line_size(); |
---|
885 | |
---|
886 | sync_ctx.mask = RTEMS_RTL_OBJ_SECT_TEXT | RTEMS_RTL_OBJ_SECT_CONST | |
---|
887 | RTEMS_RTL_OBJ_SECT_DATA | RTEMS_RTL_OBJ_SECT_BSS | |
---|
888 | RTEMS_RTL_OBJ_SECT_EH | RTEMS_RTL_OBJ_SECT_EXEC; |
---|
889 | |
---|
890 | sync_ctx.start_va = 0; |
---|
891 | sync_ctx.end_va = sync_ctx.start_va; |
---|
892 | rtems_rtl_chain_iterate (&obj->sections, |
---|
893 | rtems_rtl_obj_sect_sync_handler, |
---|
894 | &sync_ctx); |
---|
895 | |
---|
896 | if (sync_ctx.end_va != sync_ctx.start_va) |
---|
897 | { |
---|
898 | size_t size = sync_ctx.end_va - sync_ctx.start_va; |
---|
899 | rtems_cache_instruction_sync_after_code_change(sync_ctx.start_va, |
---|
900 | size); |
---|
901 | } |
---|
902 | |
---|
903 | if (obj->trampoline != NULL) |
---|
904 | { |
---|
905 | rtems_cache_instruction_sync_after_code_change(obj->trampoline, |
---|
906 | obj->tramp_size); |
---|
907 | } |
---|
908 | } |
---|
909 | |
---|
910 | bool |
---|
911 | rtems_rtl_obj_load_symbols (rtems_rtl_obj* obj, |
---|
912 | int fd, |
---|
913 | rtems_rtl_obj_sect_handler handler, |
---|
914 | void* data) |
---|
915 | { |
---|
916 | uint32_t mask = RTEMS_RTL_OBJ_SECT_SYM; |
---|
917 | return rtems_rtl_obj_section_handler (mask, obj, fd, handler, data); |
---|
918 | } |
---|
919 | |
---|
920 | static int |
---|
921 | rtems_rtl_obj_sections_linked_to_order (rtems_rtl_obj* obj, |
---|
922 | int section, |
---|
923 | uint32_t visited_mask) |
---|
924 | { |
---|
925 | rtems_chain_control* sections = &obj->sections; |
---|
926 | rtems_chain_node* node = rtems_chain_first (sections); |
---|
927 | /* |
---|
928 | * Find the section being linked-to. If the linked-to link field is 0 we have |
---|
929 | * the end and the section's order is the position we are after. |
---|
930 | */ |
---|
931 | while (!rtems_chain_is_tail (sections, node)) |
---|
932 | { |
---|
933 | rtems_rtl_obj_sect* sect = (rtems_rtl_obj_sect*) node; |
---|
934 | if (sect->section == section) |
---|
935 | { |
---|
936 | const uint32_t mask = sect->flags & RTEMS_RTL_OBJ_SECT_TYPES; |
---|
937 | int order = 0; |
---|
938 | if (sect->link != 0) |
---|
939 | { |
---|
940 | /* |
---|
941 | * Have we already visited this type of section? Avoid nesting for |
---|
942 | * ever. |
---|
943 | */ |
---|
944 | if ((sect->flags & visited_mask) != 0) |
---|
945 | { |
---|
946 | rtems_rtl_set_error (errno, "section link loop"); |
---|
947 | return -1; |
---|
948 | } |
---|
949 | return rtems_rtl_obj_sections_linked_to_order (obj, |
---|
950 | sect->link, |
---|
951 | visited_mask | mask); |
---|
952 | } |
---|
953 | node = rtems_chain_first (sections); |
---|
954 | while (!rtems_chain_is_tail (sections, node)) |
---|
955 | { |
---|
956 | sect = (rtems_rtl_obj_sect*) node; |
---|
957 | if ((sect->flags & mask) == mask) |
---|
958 | { |
---|
959 | if (sect->section == section) |
---|
960 | return order; |
---|
961 | ++order; |
---|
962 | } |
---|
963 | node = rtems_chain_next (node); |
---|
964 | } |
---|
965 | } |
---|
966 | node = rtems_chain_next (node); |
---|
967 | } |
---|
968 | rtems_rtl_set_error (errno, "section link not found"); |
---|
969 | return -1; |
---|
970 | } |
---|
971 | |
---|
972 | static void |
---|
973 | rtems_rtl_obj_sections_link_order (uint32_t mask, rtems_rtl_obj* obj) |
---|
974 | { |
---|
975 | rtems_chain_control* sections = &obj->sections; |
---|
976 | rtems_chain_node* node = rtems_chain_first (sections); |
---|
977 | int order = 0; |
---|
978 | while (!rtems_chain_is_tail (sections, node)) |
---|
979 | { |
---|
980 | rtems_rtl_obj_sect* sect = (rtems_rtl_obj_sect*) node; |
---|
981 | if ((sect->flags & mask) == mask) |
---|
982 | { |
---|
983 | /* |
---|
984 | * If the section is linked in order find the linked-to section's order |
---|
985 | * and move the section in the section list to |
---|
986 | */ |
---|
987 | if (sect->link == 0) |
---|
988 | sect->load_order = order++; |
---|
989 | else |
---|
990 | { |
---|
991 | sect->load_order = |
---|
992 | rtems_rtl_obj_sections_linked_to_order (obj, |
---|
993 | sect->link, |
---|
994 | mask); |
---|
995 | } |
---|
996 | } |
---|
997 | node = rtems_chain_next (node); |
---|
998 | } |
---|
999 | } |
---|
1000 | |
---|
1001 | static bool |
---|
1002 | rtems_rtl_obj_sections_loader (uint32_t mask, |
---|
1003 | rtems_rtl_alloc_tag tag, |
---|
1004 | rtems_rtl_obj* obj, |
---|
1005 | int fd, |
---|
1006 | uint8_t* base, |
---|
1007 | rtems_rtl_obj_sect_handler handler, |
---|
1008 | void* data) |
---|
1009 | { |
---|
1010 | rtems_chain_control* sections = &obj->sections; |
---|
1011 | rtems_chain_node* node = rtems_chain_first (sections); |
---|
1012 | size_t base_offset = 0; |
---|
1013 | int order = 0; |
---|
1014 | |
---|
1015 | if (rtems_rtl_trace (RTEMS_RTL_TRACE_LOAD_SECT)) |
---|
1016 | printf ("rtl: loading section: mask:%08" PRIx32 " base:%p\n", mask, base); |
---|
1017 | |
---|
1018 | rtems_rtl_alloc_wr_enable (tag, base); |
---|
1019 | |
---|
1020 | while (!rtems_chain_is_tail (sections, node)) |
---|
1021 | { |
---|
1022 | rtems_rtl_obj_sect* sect = (rtems_rtl_obj_sect*) node; |
---|
1023 | |
---|
1024 | if ((sect->size != 0) && ((sect->flags & mask) == mask)) |
---|
1025 | { |
---|
1026 | if (sect->load_order == order) |
---|
1027 | { |
---|
1028 | if ((sect->flags & RTEMS_RTL_OBJ_SECT_ARCH_ALLOC) == 0) |
---|
1029 | { |
---|
1030 | base_offset = rtems_rtl_obj_align (base_offset, sect->alignment); |
---|
1031 | sect->base = base + base_offset; |
---|
1032 | } |
---|
1033 | |
---|
1034 | if (rtems_rtl_trace (RTEMS_RTL_TRACE_LOAD_SECT)) |
---|
1035 | printf ("rtl: loading:%2d: %s -> %p (s:%zi f:%04" PRIx32 |
---|
1036 | " a:%" PRIu32 " l:%02d)\n", |
---|
1037 | order, sect->name, sect->base, sect->size, |
---|
1038 | sect->flags, sect->alignment, sect->link); |
---|
1039 | |
---|
1040 | if ((sect->flags & RTEMS_RTL_OBJ_SECT_LOAD) == RTEMS_RTL_OBJ_SECT_LOAD) |
---|
1041 | { |
---|
1042 | if (!handler (obj, fd, sect, data)) |
---|
1043 | { |
---|
1044 | sect->base = 0; |
---|
1045 | rtems_rtl_alloc_wr_disable (tag, base); |
---|
1046 | return false; |
---|
1047 | } |
---|
1048 | } |
---|
1049 | else if ((sect->flags & RTEMS_RTL_OBJ_SECT_ZERO) == RTEMS_RTL_OBJ_SECT_ZERO) |
---|
1050 | { |
---|
1051 | memset (sect->base, 0, sect->size); |
---|
1052 | } |
---|
1053 | else |
---|
1054 | { |
---|
1055 | /* |
---|
1056 | * This section is not to be loaded, clear the base. |
---|
1057 | */ |
---|
1058 | sect->base = 0; |
---|
1059 | } |
---|
1060 | |
---|
1061 | if (sect->base) |
---|
1062 | base_offset += sect->size; |
---|
1063 | |
---|
1064 | ++order; |
---|
1065 | |
---|
1066 | node = rtems_chain_first (sections); |
---|
1067 | continue; |
---|
1068 | } |
---|
1069 | } |
---|
1070 | |
---|
1071 | node = rtems_chain_next (node); |
---|
1072 | } |
---|
1073 | |
---|
1074 | rtems_rtl_alloc_wr_disable (tag, base); |
---|
1075 | |
---|
1076 | return true; |
---|
1077 | } |
---|
1078 | |
---|
1079 | bool |
---|
1080 | rtems_rtl_obj_alloc_sections (rtems_rtl_obj* obj, |
---|
1081 | int fd, |
---|
1082 | rtems_rtl_obj_sect_handler handler, |
---|
1083 | void* data) |
---|
1084 | { |
---|
1085 | size_t text_size; |
---|
1086 | size_t const_size; |
---|
1087 | size_t eh_size; |
---|
1088 | size_t data_size; |
---|
1089 | size_t bss_size; |
---|
1090 | |
---|
1091 | text_size = rtems_rtl_obj_text_size (obj) + rtems_rtl_obj_const_alignment (obj); |
---|
1092 | const_size = rtems_rtl_obj_const_size (obj) + rtems_rtl_obj_eh_alignment (obj); |
---|
1093 | eh_size = rtems_rtl_obj_eh_size (obj) + rtems_rtl_obj_data_alignment (obj); |
---|
1094 | data_size = rtems_rtl_obj_data_size (obj) + rtems_rtl_obj_bss_alignment (obj); |
---|
1095 | bss_size = rtems_rtl_obj_bss_size (obj); |
---|
1096 | |
---|
1097 | /* |
---|
1098 | * Set the sizes held in the object data. We need this for a fast reference. |
---|
1099 | */ |
---|
1100 | obj->text_size = text_size; |
---|
1101 | obj->const_size = const_size; |
---|
1102 | obj->data_size = data_size; |
---|
1103 | obj->eh_size = eh_size; |
---|
1104 | obj->bss_size = bss_size; |
---|
1105 | |
---|
1106 | /* |
---|
1107 | * Perform any specific allocations for sections. |
---|
1108 | */ |
---|
1109 | if (handler != NULL) |
---|
1110 | { |
---|
1111 | if (!rtems_rtl_obj_section_handler (RTEMS_RTL_OBJ_SECT_TYPES, |
---|
1112 | obj, |
---|
1113 | fd, |
---|
1114 | handler, |
---|
1115 | data)) |
---|
1116 | { |
---|
1117 | obj->exec_size = 0; |
---|
1118 | return false; |
---|
1119 | } |
---|
1120 | } |
---|
1121 | |
---|
1122 | /* |
---|
1123 | * Let the allocator manage the actual allocation. The user can use the |
---|
1124 | * standard heap or provide a specific allocator with memory protection. |
---|
1125 | */ |
---|
1126 | if (!rtems_rtl_alloc_module_new (&obj->text_base, text_size, |
---|
1127 | &obj->const_base, const_size, |
---|
1128 | &obj->eh_base, eh_size, |
---|
1129 | &obj->data_base, data_size, |
---|
1130 | &obj->bss_base, bss_size)) |
---|
1131 | { |
---|
1132 | obj->exec_size = 0; |
---|
1133 | rtems_rtl_set_error (ENOMEM, "no memory to load obj"); |
---|
1134 | return false; |
---|
1135 | } |
---|
1136 | |
---|
1137 | obj->exec_size = text_size + const_size + eh_size + data_size + bss_size; |
---|
1138 | |
---|
1139 | if (rtems_rtl_trace (RTEMS_RTL_TRACE_LOAD_SECT)) |
---|
1140 | { |
---|
1141 | printf ("rtl: load sect: text - b:%p s:%zi a:%" PRIu32 "\n", |
---|
1142 | obj->text_base, text_size, rtems_rtl_obj_text_alignment (obj)); |
---|
1143 | printf ("rtl: load sect: const - b:%p s:%zi a:%" PRIu32 "\n", |
---|
1144 | obj->const_base, const_size, rtems_rtl_obj_const_alignment (obj)); |
---|
1145 | printf ("rtl: load sect: eh - b:%p s:%zi a:%" PRIu32 "\n", |
---|
1146 | obj->eh_base, eh_size, rtems_rtl_obj_eh_alignment (obj)); |
---|
1147 | printf ("rtl: load sect: data - b:%p s:%zi a:%" PRIu32 "\n", |
---|
1148 | obj->data_base, data_size, rtems_rtl_obj_data_alignment (obj)); |
---|
1149 | printf ("rtl: load sect: bss - b:%p s:%zi a:%" PRIu32 "\n", |
---|
1150 | obj->bss_base, bss_size, rtems_rtl_obj_bss_alignment (obj)); |
---|
1151 | } |
---|
1152 | |
---|
1153 | /* |
---|
1154 | * Determine the load order. |
---|
1155 | */ |
---|
1156 | rtems_rtl_obj_sections_link_order (RTEMS_RTL_OBJ_SECT_TEXT, obj); |
---|
1157 | rtems_rtl_obj_sections_link_order (RTEMS_RTL_OBJ_SECT_CONST, obj); |
---|
1158 | rtems_rtl_obj_sections_link_order (RTEMS_RTL_OBJ_SECT_EH, obj); |
---|
1159 | rtems_rtl_obj_sections_link_order (RTEMS_RTL_OBJ_SECT_DATA, obj); |
---|
1160 | rtems_rtl_obj_sections_link_order (RTEMS_RTL_OBJ_SECT_BSS, obj); |
---|
1161 | |
---|
1162 | return true; |
---|
1163 | } |
---|
1164 | |
---|
1165 | bool |
---|
1166 | rtems_rtl_obj_load_sections (rtems_rtl_obj* obj, |
---|
1167 | int fd, |
---|
1168 | rtems_rtl_obj_sect_handler handler, |
---|
1169 | void* data) |
---|
1170 | { |
---|
1171 | /* |
---|
1172 | * Load all text, data and bsssections in seperate operations so each type of |
---|
1173 | * section is grouped together. Finish by loading any architecure specific |
---|
1174 | * sections. |
---|
1175 | */ |
---|
1176 | if (!rtems_rtl_obj_sections_loader (RTEMS_RTL_OBJ_SECT_TEXT, |
---|
1177 | rtems_rtl_alloc_text_tag (), |
---|
1178 | obj, fd, obj->text_base, handler, data) || |
---|
1179 | !rtems_rtl_obj_sections_loader (RTEMS_RTL_OBJ_SECT_CONST, |
---|
1180 | rtems_rtl_alloc_const_tag (), |
---|
1181 | obj, fd, obj->const_base, handler, data) || |
---|
1182 | !rtems_rtl_obj_sections_loader (RTEMS_RTL_OBJ_SECT_EH, |
---|
1183 | rtems_rtl_alloc_eh_tag (), |
---|
1184 | obj, fd, obj->eh_base, handler, data) || |
---|
1185 | !rtems_rtl_obj_sections_loader (RTEMS_RTL_OBJ_SECT_DATA, |
---|
1186 | rtems_rtl_alloc_data_tag (), |
---|
1187 | obj, fd, obj->data_base, handler, data) || |
---|
1188 | !rtems_rtl_obj_sections_loader (RTEMS_RTL_OBJ_SECT_BSS, |
---|
1189 | rtems_rtl_alloc_bss_tag (), |
---|
1190 | obj, fd, obj->bss_base, handler, data)) |
---|
1191 | { |
---|
1192 | rtems_rtl_alloc_module_del (&obj->text_base, &obj->const_base, &obj->eh_base, |
---|
1193 | &obj->data_base, &obj->bss_base); |
---|
1194 | obj->exec_size = 0; |
---|
1195 | return false; |
---|
1196 | } |
---|
1197 | |
---|
1198 | return true; |
---|
1199 | } |
---|
1200 | |
---|
1201 | static void |
---|
1202 | rtems_rtl_obj_run_cdtors (rtems_rtl_obj* obj, uint32_t mask) |
---|
1203 | { |
---|
1204 | rtems_chain_node* node = rtems_chain_first (&obj->sections); |
---|
1205 | while (!rtems_chain_is_tail (&obj->sections, node)) |
---|
1206 | { |
---|
1207 | rtems_rtl_obj_sect* sect = (rtems_rtl_obj_sect*) node; |
---|
1208 | if ((sect->flags & mask) == mask) |
---|
1209 | { |
---|
1210 | rtems_rtl_cdtor* handler; |
---|
1211 | size_t handlers = sect->size / sizeof (rtems_rtl_cdtor); |
---|
1212 | int c; |
---|
1213 | for (c = 0, handler = sect->base; c < handlers; ++c) |
---|
1214 | if (*handler) |
---|
1215 | (*handler) (); |
---|
1216 | } |
---|
1217 | node = rtems_chain_next (node); |
---|
1218 | } |
---|
1219 | } |
---|
1220 | |
---|
1221 | static bool |
---|
1222 | rtems_rtl_obj_cdtors_to_run (rtems_rtl_obj* obj, uint32_t mask) |
---|
1223 | { |
---|
1224 | rtems_chain_node* node = rtems_chain_first (&obj->sections); |
---|
1225 | while (!rtems_chain_is_tail (&obj->sections, node)) |
---|
1226 | { |
---|
1227 | rtems_rtl_obj_sect* sect = (rtems_rtl_obj_sect*) node; |
---|
1228 | if ((sect->flags & mask) == mask) |
---|
1229 | return true; |
---|
1230 | node = rtems_chain_next (node); |
---|
1231 | } |
---|
1232 | return false; |
---|
1233 | } |
---|
1234 | |
---|
1235 | bool |
---|
1236 | rtems_rtl_obj_ctors_to_run (rtems_rtl_obj* obj) |
---|
1237 | { |
---|
1238 | return rtems_rtl_obj_cdtors_to_run (obj, RTEMS_RTL_OBJ_SECT_CTOR); |
---|
1239 | } |
---|
1240 | |
---|
1241 | void |
---|
1242 | rtems_rtl_obj_run_ctors (rtems_rtl_obj* obj) |
---|
1243 | { |
---|
1244 | rtems_rtl_obj_run_cdtors (obj, RTEMS_RTL_OBJ_SECT_CTOR); |
---|
1245 | } |
---|
1246 | |
---|
1247 | bool |
---|
1248 | rtems_rtl_obj_dtors_to_run (rtems_rtl_obj* obj) |
---|
1249 | { |
---|
1250 | return rtems_rtl_obj_cdtors_to_run (obj, RTEMS_RTL_OBJ_SECT_DTOR); |
---|
1251 | } |
---|
1252 | |
---|
1253 | void |
---|
1254 | rtems_rtl_obj_run_dtors (rtems_rtl_obj* obj) |
---|
1255 | { |
---|
1256 | rtems_rtl_obj_run_cdtors (obj, RTEMS_RTL_OBJ_SECT_DTOR); |
---|
1257 | } |
---|
1258 | |
---|
1259 | static bool |
---|
1260 | rtems_rtl_obj_file_load (rtems_rtl_obj* obj, int fd) |
---|
1261 | { |
---|
1262 | int l; |
---|
1263 | |
---|
1264 | for (l = 0; l < (sizeof (loaders) / sizeof (rtems_rtl_loader_table)); ++l) |
---|
1265 | { |
---|
1266 | if (loaders[l].check (obj, fd)) |
---|
1267 | { |
---|
1268 | obj->format = l; |
---|
1269 | return loaders[l].load (obj, fd); |
---|
1270 | } |
---|
1271 | } |
---|
1272 | |
---|
1273 | rtems_rtl_set_error (ENOENT, "no format loader found"); |
---|
1274 | return false; |
---|
1275 | } |
---|
1276 | |
---|
1277 | static void |
---|
1278 | rtems_rtl_obj_set_error (int num, const char* text) |
---|
1279 | { |
---|
1280 | rtems_rtl_set_error (num, text); |
---|
1281 | } |
---|
1282 | |
---|
1283 | size_t |
---|
1284 | rtems_rtl_obj_get_reference (rtems_rtl_obj* obj) |
---|
1285 | { |
---|
1286 | return obj->refs; |
---|
1287 | } |
---|
1288 | |
---|
1289 | void |
---|
1290 | rtems_rtl_obj_inc_reference (rtems_rtl_obj* obj) |
---|
1291 | { |
---|
1292 | ++obj->refs; |
---|
1293 | } |
---|
1294 | |
---|
1295 | void |
---|
1296 | rtems_rtl_obj_dec_reference (rtems_rtl_obj* obj) |
---|
1297 | { |
---|
1298 | if (obj->refs) |
---|
1299 | --obj->refs; |
---|
1300 | } |
---|
1301 | |
---|
1302 | bool |
---|
1303 | rtems_rtl_obj_orphaned (rtems_rtl_obj* obj) |
---|
1304 | { |
---|
1305 | return ((obj->flags & RTEMS_RTL_OBJ_LOCKED) == 0 && |
---|
1306 | obj->users == 0 && |
---|
1307 | rtems_rtl_obj_get_reference (obj) == 0); |
---|
1308 | } |
---|
1309 | |
---|
1310 | bool |
---|
1311 | rtems_rtl_obj_load (rtems_rtl_obj* obj) |
---|
1312 | { |
---|
1313 | int fd; |
---|
1314 | |
---|
1315 | if (!rtems_rtl_obj_fname_valid (obj)) |
---|
1316 | { |
---|
1317 | rtems_rtl_set_error (ENOMEM, "invalid object file name path"); |
---|
1318 | return false; |
---|
1319 | } |
---|
1320 | |
---|
1321 | fd = open (rtems_rtl_obj_fname (obj), O_RDONLY); |
---|
1322 | if (fd < 0) |
---|
1323 | { |
---|
1324 | rtems_rtl_set_error (errno, "opening for object file"); |
---|
1325 | return false; |
---|
1326 | } |
---|
1327 | |
---|
1328 | /* |
---|
1329 | * Find the object file in the archive if it is an archive that |
---|
1330 | * has been opened. |
---|
1331 | */ |
---|
1332 | if (rtems_rtl_obj_aname_valid (obj)) |
---|
1333 | { |
---|
1334 | off_t enames = 0; |
---|
1335 | if (!rtems_rtl_obj_archive_find_obj (fd, |
---|
1336 | obj->fsize, |
---|
1337 | &obj->oname, |
---|
1338 | &obj->ooffset, |
---|
1339 | &obj->fsize, |
---|
1340 | &enames, |
---|
1341 | rtems_rtl_obj_set_error)) |
---|
1342 | { |
---|
1343 | close (fd); |
---|
1344 | return false; |
---|
1345 | } |
---|
1346 | } |
---|
1347 | |
---|
1348 | /* |
---|
1349 | * Call the format specific loader. |
---|
1350 | */ |
---|
1351 | if (!rtems_rtl_obj_file_load (obj, fd)) |
---|
1352 | { |
---|
1353 | close (fd); |
---|
1354 | return false; |
---|
1355 | } |
---|
1356 | |
---|
1357 | /* |
---|
1358 | * For GDB |
---|
1359 | */ |
---|
1360 | if (!_rtld_linkmap_add (obj)) |
---|
1361 | { |
---|
1362 | close (fd); |
---|
1363 | return false; |
---|
1364 | } |
---|
1365 | |
---|
1366 | close (fd); |
---|
1367 | |
---|
1368 | return true; |
---|
1369 | } |
---|
1370 | |
---|
1371 | bool |
---|
1372 | rtems_rtl_obj_unload (rtems_rtl_obj* obj) |
---|
1373 | { |
---|
1374 | bool ok = false; |
---|
1375 | if (obj->format >= 0 && obj->format < RTEMS_RTL_LOADERS) |
---|
1376 | { |
---|
1377 | _rtld_linkmap_delete(obj); |
---|
1378 | ok = loaders[obj->format].unload (obj); |
---|
1379 | } |
---|
1380 | else |
---|
1381 | { |
---|
1382 | rtems_rtl_set_error (EINVAL, "invalid object loader format"); |
---|
1383 | } |
---|
1384 | return ok; |
---|
1385 | } |
---|