9 * extract loadable sections from bfd file and load them
10 * @param fname bfd binary file name
11 * @param target NULL for default or string description (eg. "elf32-m68hc12")
12 * @param loader_cb section loader callback
13 * @param start_address code start address extracted from bfd
14 * @param verbose operation is silent if 0, sections are listed otherwise
15 * @return 0 if successful
17 int load_bfd(char *fname, char *target, load_section loader_cb,
18 bfd_vma *start_address, int verbose) {
21 bfd_vma lma, vma, size;
26 if ((abfd = bfd_openr(fname, target)) == NULL) {
27 bfd_perror("load_bfd: bfd_openr");
30 if (!bfd_check_format(abfd, bfd_object)) {
31 bfd_perror("load_bfd: bfd_check_format");
35 /* determine start address */
36 *start_address = bfd_get_start_address(abfd);
38 tolpc_verbose(1, "Loading %s\n"
39 "start address 0x%x\n"
40 "loading sections:\n", fname, (unsigned)*start_address);
43 for (sec = abfd->sections; sec != NULL; sec = sec->next) {
44 /* get section load address and size */
45 lma = bfd_get_section_lma(abfd, sec);
46 vma = bfd_get_section_vma(abfd, sec);
47 size = bfd_section_size(abfd, sec);
48 loadable = bfd_get_section_flags(abfd, sec) & SEC_LOAD;
49 if (!loadable || (size == 0))
52 tolpc_verbose(1, "%s : ", bfd_get_section_name(abfd, sec));
53 tolpc_verbose(1, "lma=0x%x, vma=0x%x, len=%d",
54 (unsigned)lma, (unsigned)vma, (unsigned)size);
56 /* extract section contents */
57 if ((contents = malloc(size)) == NULL) {
58 perror("load_bfd: malloc");
61 if (!bfd_get_section_contents(abfd, sec, contents, /*offs.?*/ 0, size)) {
62 bfd_perror("load_bfd: bfd_get_section_contents");
66 if (loader_cb(lma, size, contents))
71 tolpc_verbose(1, "Loading of %s finished.\n", fname);
79 int null_loader(bfd_vma lma, bfd_size_type size, void *data) {
83 main(int argc, char *argv[]) {
84 char *fname = argv[1];
87 if (!load_bfd(fname, NULL, null_loader, &start, 1))
88 printf("Nahrano, staci skocit na adresu 0x%x.\n", start);
90 printf("@#%!@$# neco se posrabilo.\n");