]> rtime.felk.cvut.cz Git - sysless.git/blob - arch/arm/mach-lpc21xx/tools/tolpc/load_bfd.c
cmdproc: Make backspace work even in sterm
[sysless.git] / arch / arm / mach-lpc21xx / tools / tolpc / load_bfd.c
1 #include <stdlib.h>
2 #include <bfd.h>
3 #include <stdio.h>
4 #include "tolpc_fn.h"
5 #include "load_bfd.h"
6
7
8 /**
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
16  */
17 int load_bfd(char *fname, char *target, load_section loader_cb,
18              bfd_vma *start_address, int verbose) {
19   bfd *abfd;
20   asection *sec;
21   bfd_vma lma, vma, size;
22   bfd_boolean loadable;
23   void *contents;
24
25   bfd_init();
26   if ((abfd = bfd_openr(fname, target)) == NULL) {
27     bfd_perror("load_bfd: bfd_openr");
28     return(1);
29   }
30   if (!bfd_check_format(abfd, bfd_object)) {
31     bfd_perror("load_bfd: bfd_check_format");
32     return(1);
33   }
34
35   /* determine start address */
36   *start_address = bfd_get_start_address(abfd);
37
38   tolpc_verbose(1, "Loading %s\n"
39                     "start address 0x%x\n"
40                     "loading sections:\n", fname, (unsigned)*start_address);
41   
42   /* load sections */
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))
50       continue;
51     
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);
55     
56     /* extract section contents */
57     if ((contents = malloc(size)) == NULL) {
58       perror("load_bfd: malloc");
59       return(1);
60     }
61     if (!bfd_get_section_contents(abfd, sec, contents, /*offs.?*/ 0, size)) {
62       bfd_perror("load_bfd: bfd_get_section_contents");
63       return(1);
64     }
65     /* call the loader */
66     if (loader_cb(lma, size, contents))
67       return(1);
68     free(contents);
69   }
70
71   tolpc_verbose(1, "Loading of %s finished.\n", fname);
72
73   bfd_close(abfd);
74   /* OK */
75   return(0);
76 }
77
78 #if TRAPARNA
79 int null_loader(bfd_vma lma, bfd_size_type size, void *data) {
80   return(0);
81 }
82
83 main(int argc, char *argv[]) {
84   char *fname = argv[1];
85   bfd_vma start;
86
87   if (!load_bfd(fname, NULL, null_loader, &start, 1))
88     printf("Nahrano, staci skocit na adresu 0x%x.\n", start);
89   else
90     printf("@#%!@$# neco se posrabilo.\n");
91 }
92 #endif