/[gcl]/gcl/bfdtest.c
ViewVC logotype

Diff of /gcl/bfdtest.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by mjthomas, Wed Oct 16 03:11:49 2002 UTC revision 1.2 by mjthomas, Wed Oct 16 03:57:53 2002 UTC
# Line 4  Line 4 
4  #include <string.h>  #include <string.h>
5  #include <stdio.h>  #include <stdio.h>
6    
7  static bfd *bself = NULL;  static bfd *exe_bfd = NULL;
8  struct bfd_link_info link_info;  struct bfd_link_info link_info;
9    
10  int build_symbol_table_bfd ( char *oname ) {  int build_symbol_table_bfd ( char *oname ) {
11    
12      int u,v;      int u,v;
   
13      asymbol **q;      asymbol **q;
14    
15      bfd_init();      if ( ! ( exe_bfd = bfd_openr ( oname, 0 ) ) ) {
   
     if ( ! ( bself = bfd_openr ( oname, 0 ) ) ) {  
16          fprintf ( stderr, "Cannot open self.\n" );          fprintf ( stderr, "Cannot open self.\n" );
17          exit ( 0 );          exit ( 0 );
18      }      }
19            
20      if (!bfd_check_format(bself,bfd_object)) {      if ( ! bfd_check_format ( exe_bfd, bfd_object ) ) {
21          fprintf ( stderr, "I'm not an object.\n" );          fprintf ( stderr, "I'm not an object.\n" );
22          exit ( 0 );          exit ( 0 );
23      }      }
24            
25      if (!(link_info.hash = bfd_link_hash_table_create (bself))) {      if (!(link_info.hash = bfd_link_hash_table_create (exe_bfd))) {
26          fprintf ( stderr, "Cannot make hash table.\n" );          fprintf ( stderr, "Cannot make hash table.\n" );
27          exit ( 0 );          exit ( 0 );
28      }      }
29            
30      if (!bfd_link_add_symbols(bself,&link_info)) {      if (!bfd_link_add_symbols(exe_bfd,&link_info)) {
31          fprintf ( stderr, "Cannot add self symbols\n.\n" );          fprintf ( stderr, "Cannot add self symbols\n.\n" );
32          exit ( 0 );          exit ( 0 );
33      }      }
34            
35      if ((u=bfd_get_symtab_upper_bound(bself))<0) {      if ((u=bfd_get_symtab_upper_bound(exe_bfd))<0) {
36          fprintf ( stderr, "Cannot get self's symtab upper bound.\n" );          fprintf ( stderr, "Cannot get self's symtab upper bound.\n" );
37          exit ( 0 );          exit ( 0 );
38      }      }
# Line 43  int build_symbol_table_bfd ( char *oname Line 40  int build_symbol_table_bfd ( char *oname
40      fprintf ( stderr, "Allocating symbol table (%d bytes)\n", u );      fprintf ( stderr, "Allocating symbol table (%d bytes)\n", u );
41      q = (asymbol **) malloc ( u );      q = (asymbol **) malloc ( u );
42    
43      if ( ( v = bfd_canonicalize_symtab ( bself, q ) ) < 0 ) {      if ( ( v = bfd_canonicalize_symtab ( exe_bfd, q ) ) < 0 ) {
44          fprintf ( stderr, "Cannot canonicalize self's symtab.\n" );          fprintf ( stderr, "Cannot canonicalize self's symtab.\n" );
45          exit ( 0 );          exit ( 0 );
46      }      }
# Line 80  int build_symbol_table_bfd ( char *oname Line 77  int build_symbol_table_bfd ( char *oname
77          }          }
78      }      }
79  #endif          #endif        
80      bfd_close(bself);      bfd_close ( exe_bfd );
81      free(q);      free(q);
82      return 0;      return 0;
83            
# Line 230  int main ( int argc, char ** argv ) Line 227  int main ( int argc, char ** argv )
227      int init_address=-1;      int init_address=-1;
228      int max_align = 0;      int max_align = 0;
229      unsigned long curr_size = 0;      unsigned long curr_size = 0;
230      bfd *b = NULL;      bfd *obj_bfd = NULL;
231      bfd_error_type myerr;      bfd_error_type myerr;
232      unsigned u = 0, v = 0;      unsigned u = 0, v = 0;
233      asymbol **q = NULL;      asymbol **q = NULL;
# Line 255  int main ( int argc, char ** argv ) Line 252  int main ( int argc, char ** argv )
252          memset ( &link_order, 0, sizeof (link_order) );          memset ( &link_order, 0, sizeof (link_order) );
253          memset ( &link_callbacks, 0, sizeof (link_callbacks) );          memset ( &link_callbacks, 0, sizeof (link_callbacks) );
254                    
255    
256            bfd_init();
257    
258          fprintf ( stderr, "BUILDING EXECUTABLE SYMBOL TABLE (ARGV[1]) \n\n" );          fprintf ( stderr, "BUILDING EXECUTABLE SYMBOL TABLE (ARGV[1]) \n\n" );
259          build_symbol_table_bfd ( argv[1] );                  build_symbol_table_bfd ( argv[1] );        
260    
# Line 272  int main ( int argc, char ** argv ) Line 272  int main ( int argc, char ** argv )
272          link_info.callbacks = &link_callbacks;          link_info.callbacks = &link_callbacks;
273          link_order.type = bfd_indirect_link_order;          link_order.type = bfd_indirect_link_order;
274    
275          if ( ! ( b = bfd_openr ( argv[2], 0 ) ) ) {          if ( ! ( obj_bfd = bfd_openr ( argv[2], 0 ) ) ) {
276              fprintf ( stderr, "Cannot open bfd.\n" );              fprintf ( stderr, "Cannot open bfd.\n" );
277          }          }
278                    
279          if ( ( myerr = bfd_get_error () ) && myerr != 3 ) {          if ( ( myerr = bfd_get_error () ) && myerr != 3 ) {
280              fprintf ( stderr, "Unknown bfd error code on openr %s %d\n.", argv[2], myerr );              fprintf ( stderr, "Unknown bfd error code on openr %s %d\n.", argv[2], myerr );
281          }          }
282          if ( ! bfd_check_format ( b, bfd_object ) ) {          fflush ( stderr );
283              fprintf ( stderr, "Unknown bfd format\n.\n" );          if ( ! bfd_check_format ( obj_bfd, bfd_object ) ) {
284                fprintf ( stderr, "Unknown bfd format %s.\n", argv[2] );
285          }          }
286    
287          if ( ( myerr = bfd_get_error () ) && myerr != 3 ) {          if ( ( myerr = bfd_get_error () ) && myerr != 3 ) {
288              fprintf ( stderr, "Unknown bfd error code on check_format\n" );              fprintf ( stderr, "Unknown bfd error code on check_format %s\n", argv[2] );
289          }          }
290    
291          bfd_set_error(0);          bfd_set_error(0);
292    
293          current=NULL;          current = NULL;
294    
295          fprintf ( stderr, "CALCULATING CURRENT, MAX_ALIGN and ALLOCATING ) \n\n" );          fprintf ( stderr, "CALCULATING CURRENT, MAX_ALIGN and ALLOCATING \n\n" );
296    
297          for (s=b->sections;s;s=s->next) {          for ( s= obj_bfd->sections;s;s=s->next) {
298    
299              s->owner=b;              s->owner = obj_bfd;
300              s->output_section=(s->flags & SEC_ALLOC) ? s : b->sections;              s->output_section = ( s->flags & SEC_ALLOC) ? s : obj_bfd->sections;
301              s->output_offset=0;              s->output_offset=0;
302    
303              if (!(s->flags & SEC_ALLOC))              if (!(s->flags & SEC_ALLOC))
# Line 341  int main ( int argc, char ** argv ) Line 342  int main ( int argc, char ** argv )
342    
343          memset ( cfd_start, 0, cfd_size );          memset ( cfd_start, 0, cfd_size );
344                    
345          for ( m = start_address, s=b->sections; s; s=s->next ) {          for ( m = start_address, s = obj_bfd->sections; s; s=s->next ) {
346    
347              if (!(s->flags & SEC_ALLOC))              if (!(s->flags & SEC_ALLOC))
348                  continue;                  continue;
# Line 358  int main ( int argc, char ** argv ) Line 359  int main ( int argc, char ** argv )
359          }          }
360    
361          fprintf ( stderr, "\n\nDOING SOMETHING WITH THE HASHED SYMBOLS\n\n" );          fprintf ( stderr, "\n\nDOING SOMETHING WITH THE HASHED SYMBOLS\n\n" );
362          if ((u=bfd_get_symtab_upper_bound(b))<0)          if ((u=bfd_get_symtab_upper_bound(obj_bfd))<0)
363              fprintf ( stderr, "Cannot get symtab uppoer bound.\n" );              fprintf ( stderr, "Cannot get symtab uppoer bound.\n" );
364          q = (asymbol **) alloca ( u );          q = (asymbol **) alloca ( u );
365          if ( ( v = bfd_canonicalize_symtab ( b, q ) ) < 0 )          if ( ( v = bfd_canonicalize_symtab ( obj_bfd, q ) ) < 0 )
366              fprintf ( stderr, "cannot canonicalize symtab.\n" );              fprintf ( stderr, "cannot canonicalize symtab.\n" );
367          fprintf ( stderr, "u = %d, v = %d\n", u, v );          fprintf ( stderr, "u = %d, v = %d\n", u, v );
368          for (u=0;u<v;u++) {          for (u=0;u<v;u++) {
# Line 389  int main ( int argc, char ** argv ) Line 390  int main ( int argc, char ** argv )
390    
391          }          }
392    
393          fprintf ( stderr, "\n\nDOING RELOCATIONS", cfd_size );          fprintf ( stderr, "\n\nDOING RELOCATIONS\n\n", cfd_size );
394          fflush ( stderr );          fflush ( stderr );
395          for (s=b->sections;s;s=s->next) {          for ( s = obj_bfd->sections; s; s = s->next ) {
396    
397              fprintf ( stderr, "s->name %s, s->flags = %x\n", s->name, s->flags );              fprintf ( stderr, "s->name %s, s->flags = %x\n", s->name, s->flags );
398              if (!(s->flags & SEC_LOAD))              if ( ! ( s->flags & SEC_LOAD ) )
399                  continue;                  continue;
400    
401              link_order.u.indirect.section=s;              link_order.u.indirect.section=s;
402    
403              fprintf ( stderr, "About to get reloc section contents\n" );              fprintf ( stderr, "About to get reloc section contents\n" );
404    
405              fprintf ( stderr, "b = %x, section %s, s->output_section = %x, q = %x\n",              fprintf ( stderr, "obj_bfd = %x, section %s, s->output_section = %x, q = %x\n",
406                        b, s->name, s->output_section, q);                        obj_bfd, s->name, s->output_section, q);
407    
408              fflush ( stderr );              fflush ( stderr );
409              if (!bfd_get_relocated_section_contents(b,&link_info,&link_order,              if (!bfd_get_relocated_section_contents(obj_bfd, &link_info,&link_order,
410                                                       (void *)(unsigned long)s->output_section->vma,0,q))                                                       (void *)(unsigned long)s->output_section->vma,0,q))
411                  fprintf ( stderr, "Cannot get relocated section contents\n");                  fprintf ( stderr, "Cannot get relocated section contents\n");
412                            
413          }          }
414                    
415          bfd_close(b);          bfd_close ( obj_bfd );
416          printf("start address -T %x \n", cfd_start);          printf("start address -T %x \n", cfd_start);
417      }      }
418  }  }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26