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