toolsandutils/e32tools/elftools/getexports/getdirective.cpp
changeset 0 83f4b4db085c
child 1 d4b442d23379
equal deleted inserted replaced
-1:000000000000 0:83f4b4db085c
       
     1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include <stdio.h>
       
    17 #include <string.h>
       
    18 #include <stdlib.h>
       
    19 
       
    20 #include <tools/elfdefs.h>
       
    21 
       
    22 #define ADDR(rtype, p, o) (rtype *)(((char *)p) + o)
       
    23 
       
    24 void croak(char * s)
       
    25 {
       
    26   printf("GETDIRECTIVE ERROR: %s\n", s);
       
    27   exit(EXIT_FAILURE);
       
    28 }
       
    29 
       
    30 void croak2(char * s1, char * s2)
       
    31 {
       
    32   printf("GETDIRECTIVE ERROR: %s %s\n", s1, s2);
       
    33   exit(EXIT_FAILURE);
       
    34 }
       
    35 
       
    36 int GetFileSize(FILE * f, char * file)
       
    37 {
       
    38   int r = fseek(f, 0, SEEK_END);
       
    39   if (r) croak2("can't seek to end of file:", file);
       
    40   r = ftell(f);
       
    41   rewind(f);
       
    42   return r;
       
    43 }
       
    44 
       
    45 void EnsureElf(Elf32_Ehdr* ef)
       
    46 	{
       
    47 	// Do nothing for now
       
    48 	(void)ef;
       
    49 	}
       
    50 
       
    51 Elf32_Ehdr* OpenElfFile (char* file)
       
    52 	{
       
    53 	Elf32_Ehdr* eh;
       
    54 	FILE* f = fopen(file, "rb");
       
    55 	if (!f)
       
    56 		croak2("can't open file:", file);
       
    57 
       
    58 	int fsize = GetFileSize(f, file);
       
    59 
       
    60 	eh = (Elf32_Ehdr*)malloc(fsize);
       
    61 	if (!eh)
       
    62 		croak("Out of memory");
       
    63 
       
    64 	if (fread(eh, fsize, 1, f) != 1)
       
    65 		croak2("Can't read file", file);
       
    66 
       
    67 	EnsureElf(eh);
       
    68 
       
    69 	return eh;
       
    70 	}
       
    71 
       
    72 void PrintDirectiveSections (Elf32_Ehdr * eh)
       
    73 {
       
    74   int shoff = eh->e_shoff;                      // offset of section header table
       
    75   if (shoff) {
       
    76     Elf32_Shdr * shdr = ADDR(Elf32_Shdr, eh, shoff);
       
    77 
       
    78     int shnum = eh->e_shnum;                    // number of section headers
       
    79 
       
    80 	// e_shnum will be 0 if the number of sections is >= SHN_LORESERVE (0xff00)
       
    81 	// If this is the case, sh_size contains the actual number of section headers.
       
    82 	// If sh_size is 0, there really aren't any section headers.
       
    83 	if (!shnum)
       
    84 		shnum = shdr->sh_size;
       
    85 
       
    86     int shstrndx = eh->e_shstrndx;
       
    87 
       
    88 	// If the section name string table index is >= SHN_LORESERVE (0xff00), shstrndx
       
    89 	// contains SHN_XINDEX (0xffff).
       
    90 	// If this is the case, sh_link contains the actual index of the section name string
       
    91 	// table, otherwise sh_link is 0.
       
    92 
       
    93 	if (shstrndx >= 65535)
       
    94 		shstrndx = shdr->sh_link;
       
    95 
       
    96     int snameoffset = shdr[shstrndx].sh_offset; // offset in file of sections' names
       
    97     char * sname = ADDR(char, eh, snameoffset);
       
    98     for (int i = 0; i < shnum; i++) {
       
    99       if (i != shstrndx) {
       
   100 	if (!strcmp(".directive", &sname[shdr[i].sh_name])) {
       
   101 	  // we're in business. print the section to stdout
       
   102 	  char * data = ADDR(char, eh, shdr[i].sh_offset);
       
   103 	  int size = shdr[i].sh_size;
       
   104 	  fwrite(data, size, 1, stdout);
       
   105 	  printf("\n");
       
   106 	}
       
   107       }
       
   108     }
       
   109   }
       
   110 }
       
   111 	
       
   112 int main(int argc, char** argv)
       
   113 	{
       
   114 	(void)argc;
       
   115 	char* file = argv[1];
       
   116 	PrintDirectiveSections(OpenElfFile(file));
       
   117 
       
   118 	return EXIT_SUCCESS;
       
   119 	}
       
   120   
       
   121 
       
   122   
       
   123 
       
   124   
       
   125