1
0

elfcomm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /*++
  2. Copyright (c) 2016 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. elfcomm.c
  5. Abstract:
  6. This module implements ELF support functions that are agnostic to the
  7. address size (the same in 32-bit or 64-bit).
  8. Author:
  9. Evan Green 8-Apr-2016
  10. Environment:
  11. Any
  12. --*/
  13. //
  14. // ------------------------------------------------------------------- Includes
  15. //
  16. //
  17. // Note that elfn.h is not included here because all functions in this file
  18. // should be agnostic to 32 vs 64 bit. Needing to include that headers means
  19. // the function belongs in a different file.
  20. //
  21. #include "imp.h"
  22. #include "elf.h"
  23. //
  24. // ---------------------------------------------------------------- Definitions
  25. //
  26. //
  27. // ------------------------------------------------------ Data Type Definitions
  28. //
  29. typedef enum _ELF_LIBRARY_PATH_VARIABLE {
  30. ElfLibraryPathOrigin,
  31. ElfLibraryPathLib,
  32. ElfLibraryPathPlatform
  33. } ELF_LIBRARY_PATH_VARIABLE, *PELF_LIBRARY_PATH_VARIABLE;
  34. /*++
  35. Structure Description:
  36. This structure stores an entry in the table of variables that can be
  37. substituted in ELF library paths.
  38. Members:
  39. Variable - Stores the variable code.
  40. Name - Stores the variable name.
  41. --*/
  42. typedef struct _ELF_LIBRARY_PATH_VARIABLE_ENTRY {
  43. ELF_LIBRARY_PATH_VARIABLE Variable;
  44. PSTR Name;
  45. } ELF_LIBRARY_PATH_VARIABLE_ENTRY, *PELF_LIBRARY_PATH_VARIABLE_ENTRY;
  46. //
  47. // ----------------------------------------------- Internal Function Prototypes
  48. //
  49. KSTATUS
  50. ImpElfPerformLibraryPathSubstitutions (
  51. PLOADED_IMAGE Image,
  52. PSTR *Path,
  53. PUINTN PathCapacity
  54. );
  55. //
  56. // -------------------------------------------------------------------- Globals
  57. //
  58. ELF_LIBRARY_PATH_VARIABLE_ENTRY ElfLibraryPathVariables[] = {
  59. {ElfLibraryPathOrigin, "ORIGIN"},
  60. {ElfLibraryPathLib, "LIB"},
  61. {ElfLibraryPathPlatform, "PLATFORM"}
  62. };
  63. //
  64. // ------------------------------------------------------------------ Functions
  65. //
  66. KSTATUS
  67. ImpElfOpenWithPathList (
  68. PLOADED_IMAGE Parent,
  69. PSTR LibraryName,
  70. PSTR PathList,
  71. PIMAGE_FILE_INFORMATION File,
  72. PSTR *Path
  73. )
  74. /*++
  75. Routine Description:
  76. This routine attempts to load a needed library for an ELF image.
  77. Arguments:
  78. Parent - Supplies a pointer to the image that needs the library.
  79. LibraryName - Supplies the name of the library to load.
  80. PathList - Supplies a colon-separated list of paths to try.
  81. File - Supplies a pointer where the information for the file including its
  82. open handle will be returned.
  83. Path - Supplies a pointer where the real path to the opened file will be
  84. returned. The caller is responsible for freeing this memory.
  85. Return Value:
  86. Status code.
  87. --*/
  88. {
  89. PSTR CompletePath;
  90. UINTN CompletePathCapacity;
  91. UINTN CompletePathSize;
  92. PSTR CurrentPath;
  93. UINTN LibraryLength;
  94. PSTR NextSeparator;
  95. UINTN PrefixLength;
  96. KSTATUS Status;
  97. LibraryLength = RtlStringLength(LibraryName);
  98. CompletePath = NULL;
  99. CompletePathCapacity = 0;
  100. CurrentPath = PathList;
  101. while (TRUE) {
  102. NextSeparator = RtlStringFindCharacter(CurrentPath, ':', -1);
  103. if (NextSeparator == NULL) {
  104. PrefixLength = RtlStringLength(CurrentPath);
  105. } else {
  106. PrefixLength = (UINTN)NextSeparator - (UINTN)CurrentPath;
  107. }
  108. //
  109. // The complete path is "prefix/library". Reallocate the buffer if
  110. // needed.
  111. //
  112. CompletePathSize = PrefixLength + LibraryLength + 2;
  113. if (CompletePathSize > CompletePathCapacity) {
  114. if (CompletePath != NULL) {
  115. ImFreeMemory(CompletePath);
  116. }
  117. CompletePath = ImAllocateMemory(CompletePathSize,
  118. IM_ALLOCATION_TAG);
  119. if (CompletePath == NULL) {
  120. Status = STATUS_INSUFFICIENT_RESOURCES;
  121. break;
  122. }
  123. }
  124. if (PrefixLength != 0) {
  125. RtlCopyMemory(CompletePath, CurrentPath, PrefixLength);
  126. if (CompletePath[PrefixLength - 1] != '/') {
  127. CompletePath[PrefixLength] = '/';
  128. PrefixLength += 1;
  129. }
  130. }
  131. RtlCopyMemory(CompletePath + PrefixLength,
  132. LibraryName,
  133. LibraryLength);
  134. CompletePath[PrefixLength + LibraryLength] = '\0';
  135. Status = ImpElfPerformLibraryPathSubstitutions(Parent,
  136. &CompletePath,
  137. &CompletePathCapacity);
  138. if (!KSUCCESS(Status)) {
  139. break;
  140. }
  141. Status = ImOpenFile(Parent->SystemContext, CompletePath, File);
  142. if (KSUCCESS(Status)) {
  143. break;
  144. }
  145. if (NextSeparator == NULL) {
  146. break;
  147. }
  148. CurrentPath = NextSeparator + 1;
  149. }
  150. //
  151. // If the file could be opened, get its real path.
  152. //
  153. if (KSUCCESS(Status)) {
  154. if (Path != NULL) {
  155. *Path = CompletePath;
  156. CompletePath = NULL;
  157. }
  158. }
  159. if (CompletePath != NULL) {
  160. ImFreeMemory(CompletePath);
  161. }
  162. return Status;
  163. }
  164. ULONG
  165. ImpElfOriginalHash (
  166. PSTR SymbolName
  167. )
  168. /*++
  169. Routine Description:
  170. This routine hashes a symbol name to get the index into the ELF hash table.
  171. Arguments:
  172. SymbolName - Supplies a pointer to the name to hash.
  173. Return Value:
  174. Returns the hash of the name.
  175. --*/
  176. {
  177. ULONG Hash;
  178. ULONG Temporary;
  179. Hash = 0;
  180. while (*SymbolName != '\0') {
  181. Hash = (Hash << 4) + *SymbolName;
  182. Temporary = Hash & 0xF0000000;
  183. if (Temporary != 0) {
  184. Hash ^= Temporary >> 24;
  185. }
  186. Hash &= ~Temporary;
  187. SymbolName += 1;
  188. }
  189. return Hash;
  190. }
  191. ULONG
  192. ImpElfGnuHash (
  193. PSTR SymbolName
  194. )
  195. /*++
  196. Routine Description:
  197. This routine hashes a symbol name to get the index into the ELF hash table
  198. using the GNU style hash function.
  199. Arguments:
  200. SymbolName - Supplies a pointer to the name to hash.
  201. Return Value:
  202. Returns the hash of the name.
  203. --*/
  204. {
  205. ULONG Hash;
  206. Hash = 5381;
  207. while (*SymbolName != '\0') {
  208. //
  209. // It's really hash * 33 + Character, but multiply by 33 is expanded
  210. // into multiply by 32 plus one.
  211. //
  212. Hash = ((Hash << 5) + Hash) + (UCHAR)*SymbolName;
  213. SymbolName += 1;
  214. }
  215. return Hash;
  216. }
  217. PSTR
  218. ImpElfGetEnvironmentVariable (
  219. PSTR Variable
  220. )
  221. /*++
  222. Routine Description:
  223. This routine gets an environment variable value for the image library.
  224. Arguments:
  225. Variable - Supplies a pointer to a null terminated string containing the
  226. name of the variable to get.
  227. Return Value:
  228. Returns a pointer to the value of the environment variable. The image
  229. library will not free or modify this value.
  230. NULL if the given environment variable is not set.
  231. --*/
  232. {
  233. if (ImGetEnvironmentVariable != NULL) {
  234. return ImGetEnvironmentVariable(Variable);
  235. }
  236. return NULL;
  237. }
  238. //
  239. // --------------------------------------------------------- Internal Functions
  240. //
  241. KSTATUS
  242. ImpElfPerformLibraryPathSubstitutions (
  243. PLOADED_IMAGE Image,
  244. PSTR *Path,
  245. PUINTN PathCapacity
  246. )
  247. /*++
  248. Routine Description:
  249. This routine performs any variable substitutions in a library path.
  250. Arguments:
  251. Image - Supplies a pointer to the image loading the library (not the
  252. library itself obviously, that hasn't been loaded yet).
  253. Path - Supplies a pointer that on input contains the complete path. On
  254. output this will contain the complete path with variables expanded.
  255. PathCapacity - Supplies a pointer that on input contains the size of the
  256. path allocation. This will be updated on output if the string is
  257. reallocated.
  258. Return Value:
  259. STATUS_SUCCESS on success.
  260. STATUS_INSUFFICIENT_RESOURCES if memory could not be allocated.
  261. --*/
  262. {
  263. PSTR CurrentPath;
  264. PSTR CurrentVariable;
  265. UINTN Delta;
  266. PELF_LIBRARY_PATH_VARIABLE_ENTRY Entry;
  267. UINTN EntryCount;
  268. UINTN EntryIndex;
  269. UINTN Index;
  270. PSTR Name;
  271. UINTN NameLength;
  272. PSTR NewBuffer;
  273. PSTR Replacement;
  274. UINTN ReplacementLength;
  275. UINTN ReplaceSize;
  276. UINTN ReplaceStart;
  277. KSTATUS Status;
  278. EntryCount = sizeof(ElfLibraryPathVariables) /
  279. sizeof(ElfLibraryPathVariables[0]);
  280. CurrentVariable = RtlStringFindCharacter(*Path, '$', -1);
  281. while (CurrentVariable != NULL) {
  282. //
  283. // Find the name of the variable and the size of the region to replace.
  284. //
  285. ReplaceStart = (UINTN)CurrentVariable - (UINTN)(*Path);
  286. CurrentVariable += 1;
  287. if (*CurrentVariable == '{') {
  288. CurrentVariable += 1;
  289. Name = CurrentVariable;
  290. while ((*CurrentVariable != '\0') && (*CurrentVariable != '}')) {
  291. CurrentVariable += 1;
  292. }
  293. if (*CurrentVariable != '}') {
  294. RtlDebugPrint("ELF: Missing closing brace on %s.\n", *Path);
  295. Status = STATUS_INVALID_SEQUENCE;
  296. goto ElfPerformLibraryPathSubstitutionsEnd;
  297. }
  298. NameLength = (UINTN)CurrentVariable - (UINTN)Name;
  299. CurrentVariable += 1;
  300. } else {
  301. Name = CurrentVariable;
  302. while (RtlIsCharacterAlphabetic(*CurrentVariable) != FALSE) {
  303. CurrentVariable += 1;
  304. }
  305. NameLength = (UINTN)CurrentVariable - (UINTN)Name;
  306. }
  307. ReplaceSize = (UINTN)CurrentVariable - (UINTN)ReplaceStart;
  308. //
  309. // Decode the variable.
  310. //
  311. for (EntryIndex = 0; EntryIndex < EntryCount; EntryIndex += 1) {
  312. Entry = &(ElfLibraryPathVariables[EntryIndex]);
  313. if ((RtlAreStringsEqual(Name, Entry->Name, NameLength) != FALSE) &&
  314. (Entry->Name[NameLength] == '\0')) {
  315. break;
  316. }
  317. }
  318. if (EntryIndex == EntryCount) {
  319. RtlDebugPrint("ELF: Warning: Unknown variable starting at %s.\n",
  320. Name);
  321. } else {
  322. //
  323. // TODO: Get the correct variable values.
  324. //
  325. ASSERT(FALSE);
  326. switch (Entry->Variable) {
  327. case ElfLibraryPathOrigin:
  328. Replacement = ".";
  329. break;
  330. case ElfLibraryPathLib:
  331. Replacement = "lib";
  332. break;
  333. case ElfLibraryPathPlatform:
  334. Replacement = "i386";
  335. break;
  336. default:
  337. ASSERT(FALSE);
  338. Replacement = ".";
  339. break;
  340. }
  341. ReplacementLength = RtlStringLength(Replacement);
  342. //
  343. // If the replacement is shorter than the original, then just
  344. // copy the replacement over followed by the rest.
  345. //
  346. if (ReplacementLength <= ReplaceSize) {
  347. CurrentPath = *Path;
  348. RtlCopyMemory(CurrentPath + ReplaceStart,
  349. Replacement,
  350. ReplacementLength);
  351. Delta = ReplaceSize - ReplacementLength;
  352. if (Delta != 0) {
  353. for (Index = ReplaceStart + ReplaceSize;
  354. Index < *PathCapacity - Delta;
  355. Index += 1) {
  356. CurrentPath[Index] = CurrentPath[Index + Delta];
  357. }
  358. CurrentVariable -= Delta;
  359. }
  360. //
  361. // The replacement is bigger than the region it's replacing.
  362. //
  363. } else {
  364. Delta = ReplacementLength - ReplaceSize;
  365. NewBuffer = ImAllocateMemory(*PathCapacity + Delta,
  366. IM_ALLOCATION_TAG);
  367. if (NewBuffer == NULL) {
  368. Status = STATUS_INSUFFICIENT_RESOURCES;
  369. goto ElfPerformLibraryPathSubstitutionsEnd;
  370. }
  371. RtlCopyMemory(NewBuffer, *Path, ReplaceStart);
  372. RtlCopyMemory(NewBuffer + ReplaceStart,
  373. Replacement,
  374. ReplacementLength);
  375. RtlCopyMemory(NewBuffer + ReplaceStart + ReplacementLength,
  376. *Path + ReplaceSize,
  377. *PathCapacity - (ReplaceStart + ReplaceSize));
  378. CurrentVariable = (PSTR)((UINTN)CurrentVariable -
  379. (UINTN)(*Path) +
  380. (UINTN)NewBuffer);
  381. ImFreeMemory(*Path);
  382. *PathCapacity += Delta;
  383. }
  384. }
  385. //
  386. // Find the next variable.
  387. //
  388. CurrentVariable = RtlStringFindCharacter(CurrentVariable, '$', -1);
  389. }
  390. Status = STATUS_SUCCESS;
  391. ElfPerformLibraryPathSubstitutionsEnd:
  392. return Status;
  393. }