HPCToolkit
ElfHelper.cpp
Go to the documentation of this file.
1 // * BeginRiceCopyright *****************************************************
2 //
3 // $HeadURL$
4 // $Id$
5 //
6 // --------------------------------------------------------------------------
7 // Part of HPCToolkit (hpctoolkit.org)
8 //
9 // Information about sources of support for research and development of
10 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
11 // --------------------------------------------------------------------------
12 //
13 // Copyright ((c)) 2002-2019, Rice University
14 // All rights reserved.
15 //
16 // Redistribution and use in source and binary forms, with or without
17 // modification, are permitted provided that the following conditions are
18 // met:
19 //
20 // * Redistributions of source code must retain the above copyright
21 // notice, this list of conditions and the following disclaimer.
22 //
23 // * Redistributions in binary form must reproduce the above copyright
24 // notice, this list of conditions and the following disclaimer in the
25 // documentation and/or other materials provided with the distribution.
26 //
27 // * Neither the name of Rice University (RICE) nor the names of its
28 // contributors may be used to endorse or promote products derived from
29 // this software without specific prior written permission.
30 //
31 // This software is provided by RICE and contributors "as is" and any
32 // express or implied warranties, including, but not limited to, the
33 // implied warranties of merchantability and fitness for a particular
34 // purpose are disclaimed. In no event shall RICE or contributors be
35 // liable for any direct, indirect, incidental, special, exemplary, or
36 // consequential damages (including, but not limited to, procurement of
37 // substitute goods or services; loss of use, data, or profits; or
38 // business interruption) however caused and on any theory of liability,
39 // whether in contract, strict liability, or tort (including negligence
40 // or otherwise) arising in any way out of the use of this software, even
41 // if advised of the possibility of such damage.
42 //
43 // ******************************************************* EndRiceCopyright *
44 
45 
46 //***************************************************************************
47 //
48 // File: ElfHelper.cpp
49 //
50 // Purpose:
51 // implementation of a function that scans an elf file and returns a vector
52 // of sections
53 //
54 //***************************************************************************
55 
56 
57 //******************************************************************************
58 // system includes
59 //******************************************************************************
60 
61 #include <err.h>
62 #include <gelf.h>
63 
64 
65 
66 //******************************************************************************
67 // local includes
68 //******************************************************************************
69 
71 
72 #include "ElfHelper.hpp"
73 #include "RelocateCubin.hpp"
74 
75 #include <Elf_X.h> // ensure EM_CUDA defined
76 
77 #include <include/hpctoolkit-config.h>
78 
79 
80 //******************************************************************************
81 // macros
82 //******************************************************************************
83 
84 #ifndef NULL
85 #define NULL 0
86 #endif
87 
88 
89 
90 //******************************************************************************
91 // interface functions
92 //******************************************************************************
93 
94 bool
96 (
97  char *_memPtr,
98  size_t _memLen,
99  std::string _fileName
100 )
101 {
102  memPtr = _memPtr;
103  memLen = _memLen;
104  fileName = _fileName;
105 
106  elf_version(EV_CURRENT);
107  elf = elf_memory(memPtr, memLen);
108  if (elf == 0 || elf_kind(elf) != ELF_K_ELF) {
109  return false;
110  }
111  GElf_Ehdr ehdr_v;
112  GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_v);
113  if (!ehdr) {
114  return false;
115  }
116 #ifdef EM_CUDA
117 
118  if (ehdr->e_machine == EM_CUDA) {
119 #ifdef DYNINST_USE_CUDA
120  relocateCubin(memPtr, elf);
121 #else
122  elf_end(elf);
123  return false;
124 #endif
125  }
126 
127 #endif
128 
129  return true;
130 }
131 
132 
134 {
135  elf_end(elf);
136 }
137 
138 
139 // assemble a vector of pointers to each section in an elf binary
142 (
143  Elf *elf
144 )
145 {
146  ElfSectionVector *sections = new ElfSectionVector;
147  bool nonempty = false;
148  if (elf) {
149  Elf_Scn *scn = NULL;
150  while ((scn = elf_nextscn(elf, scn)) != NULL) {
151  sections->push_back(scn);
152  nonempty = true;
153  }
154  }
155  if (nonempty) return sections;
156  else {
157  delete sections;
158  return NULL;
159  }
160 }
161 
162 
163 char *
165 (
166  char *obj_ptr,
167  GElf_Shdr *shdr
168 )
169 {
170  char *sectionData = obj_ptr + shdr->sh_offset;
171  return sectionData;
172 }
Elf * elf
Definition: ElfHelper.hpp:92
bool relocateCubin(char *cubin_ptr, Elf *cubin_elf)
bool open(char *_memPtr, size_t _memLen, std::string _fileName)
Definition: ElfHelper.cpp:96
ElfSectionVector * elfGetSectionVector(Elf *elf)
Definition: ElfHelper.cpp:142
#define NULL
Definition: ElfHelper.cpp:85
char * elfSectionGetData(char *obj_ptr, GElf_Shdr *shdr)
Definition: ElfHelper.cpp:165