ruby-staci 2.2.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. checksums.yaml +7 -0
  2. data/.yardopts +14 -0
  3. data/COPYING +30 -0
  4. data/COPYING_old +64 -0
  5. data/ChangeLog +3826 -0
  6. data/Makefile +92 -0
  7. data/NEWS +1194 -0
  8. data/README.md +66 -0
  9. data/dist-files +113 -0
  10. data/docs/bind-array-to-in_cond.md +38 -0
  11. data/docs/conflicts-local-connections-and-processes.md +98 -0
  12. data/docs/hanging-after-inactivity.md +63 -0
  13. data/docs/install-binary-package.md +44 -0
  14. data/docs/install-full-client.md +111 -0
  15. data/docs/install-instant-client.md +194 -0
  16. data/docs/install-on-osx.md +133 -0
  17. data/docs/ldap-auth-and-function-interposition.md +123 -0
  18. data/docs/number-type-mapping.md +79 -0
  19. data/docs/osx-install-dev-tools.png +0 -0
  20. data/docs/platform-specific-issues.md +164 -0
  21. data/docs/report-installation-issue.md +50 -0
  22. data/docs/timeout-parameters.md +94 -0
  23. data/ext/oci8/.document +18 -0
  24. data/ext/oci8/MANIFEST +18 -0
  25. data/ext/oci8/apiwrap.c.tmpl +178 -0
  26. data/ext/oci8/apiwrap.h.tmpl +61 -0
  27. data/ext/oci8/apiwrap.rb +96 -0
  28. data/ext/oci8/apiwrap.yml +1322 -0
  29. data/ext/oci8/attr.c +57 -0
  30. data/ext/oci8/bind.c +838 -0
  31. data/ext/oci8/connection_pool.c +216 -0
  32. data/ext/oci8/encoding.c +196 -0
  33. data/ext/oci8/env.c +139 -0
  34. data/ext/oci8/error.c +385 -0
  35. data/ext/oci8/extconf.rb +219 -0
  36. data/ext/oci8/hook_funcs.c +407 -0
  37. data/ext/oci8/lob.c +1278 -0
  38. data/ext/oci8/metadata.c +279 -0
  39. data/ext/oci8/object.c +919 -0
  40. data/ext/oci8/oci8.c +1058 -0
  41. data/ext/oci8/oci8.h +556 -0
  42. data/ext/oci8/oci8lib.c +704 -0
  43. data/ext/oci8/ocidatetime.c +506 -0
  44. data/ext/oci8/ocihandle.c +852 -0
  45. data/ext/oci8/ocinumber.c +1922 -0
  46. data/ext/oci8/oraconf.rb +1145 -0
  47. data/ext/oci8/oradate.c +670 -0
  48. data/ext/oci8/oranumber_util.c +352 -0
  49. data/ext/oci8/oranumber_util.h +24 -0
  50. data/ext/oci8/plthook.h +66 -0
  51. data/ext/oci8/plthook_elf.c +702 -0
  52. data/ext/oci8/plthook_osx.c +505 -0
  53. data/ext/oci8/plthook_win32.c +391 -0
  54. data/ext/oci8/post-config.rb +5 -0
  55. data/ext/oci8/stmt.c +448 -0
  56. data/ext/oci8/thread_util.c +81 -0
  57. data/ext/oci8/thread_util.h +18 -0
  58. data/ext/oci8/util.c +71 -0
  59. data/ext/oci8/win32.c +117 -0
  60. data/lib/.document +1 -0
  61. data/lib/dbd/STACI.rb +591 -0
  62. data/lib/oci8/.document +8 -0
  63. data/lib/oci8/bindtype.rb +333 -0
  64. data/lib/oci8/check_load_error.rb +146 -0
  65. data/lib/oci8/compat.rb +117 -0
  66. data/lib/oci8/connection_pool.rb +179 -0
  67. data/lib/oci8/cursor.rb +605 -0
  68. data/lib/oci8/datetime.rb +605 -0
  69. data/lib/oci8/encoding-init.rb +45 -0
  70. data/lib/oci8/encoding.yml +537 -0
  71. data/lib/oci8/metadata.rb +2148 -0
  72. data/lib/oci8/object.rb +641 -0
  73. data/lib/oci8/oci8.rb +756 -0
  74. data/lib/oci8/ocihandle.rb +591 -0
  75. data/lib/oci8/oracle_version.rb +153 -0
  76. data/lib/oci8/properties.rb +196 -0
  77. data/lib/oci8/version.rb +3 -0
  78. data/lib/ruby-staci.rb +1 -0
  79. data/lib/staci.rb +190 -0
  80. data/metaconfig +142 -0
  81. data/pre-distclean.rb +7 -0
  82. data/ruby-aci.gemspec +83 -0
  83. data/setup.rb +1342 -0
  84. data/test/README.md +37 -0
  85. data/test/config.rb +201 -0
  86. data/test/setup_test_object.sql +199 -0
  87. data/test/setup_test_package.sql +59 -0
  88. data/test/test_all.rb +56 -0
  89. data/test/test_appinfo.rb +62 -0
  90. data/test/test_array_dml.rb +333 -0
  91. data/test/test_bind_array.rb +70 -0
  92. data/test/test_bind_boolean.rb +99 -0
  93. data/test/test_bind_integer.rb +47 -0
  94. data/test/test_bind_raw.rb +45 -0
  95. data/test/test_bind_string.rb +105 -0
  96. data/test/test_bind_time.rb +177 -0
  97. data/test/test_break.rb +124 -0
  98. data/test/test_clob.rb +86 -0
  99. data/test/test_connection_pool.rb +124 -0
  100. data/test/test_connstr.rb +220 -0
  101. data/test/test_datetime.rb +585 -0
  102. data/test/test_dbi.rb +365 -0
  103. data/test/test_dbi_clob.rb +53 -0
  104. data/test/test_encoding.rb +103 -0
  105. data/test/test_error.rb +87 -0
  106. data/test/test_metadata.rb +2674 -0
  107. data/test/test_object.rb +546 -0
  108. data/test/test_oci8.rb +624 -0
  109. data/test/test_oracle_version.rb +68 -0
  110. data/test/test_oradate.rb +255 -0
  111. data/test/test_oranumber.rb +786 -0
  112. data/test/test_package_type.rb +981 -0
  113. data/test/test_properties.rb +17 -0
  114. data/test/test_rowid.rb +32 -0
  115. metadata +158 -0
@@ -0,0 +1,702 @@
1
+ /* -*- indent-tabs-mode: nil -*-
2
+ *
3
+ * plthook_elf.c -- implemention of plthook for ELF format
4
+ *
5
+ * URL: https://github.com/kubo/plthook
6
+ *
7
+ * ------------------------------------------------------
8
+ *
9
+ * Copyright 2013-2016 Kubo Takehiro <kubo@jiubao.org>
10
+ *
11
+ * Redistribution and use in source and binary forms, with or without modification, are
12
+ * permitted provided that the following conditions are met:
13
+ *
14
+ * 1. Redistributions of source code must retain the above copyright notice, this list of
15
+ * conditions and the following disclaimer.
16
+ *
17
+ * 2. Redistributions in binary form must reproduce the above copyright notice, this list
18
+ * of conditions and the following disclaimer in the documentation and/or other materials
19
+ * provided with the distribution.
20
+ *
21
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS OR IMPLIED
22
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
23
+ * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
24
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
27
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
+ *
31
+ * The views and conclusions contained in the software and documentation are those of the
32
+ * authors and should not be interpreted as representing official policies, either expressed
33
+ * or implied, of the authors.
34
+ *
35
+ */
36
+ #if defined(__sun) && defined(_XOPEN_SOURCE) && !defined(__EXTENSIONS__)
37
+ #define __EXTENSIONS__
38
+ #endif
39
+ #if defined(__linux__) && !defined(_GNU_SOURCE)
40
+ #define _GNU_SOURCE
41
+ #endif
42
+ #include <stdio.h>
43
+ #include <stdarg.h>
44
+ #include <stdlib.h>
45
+ #include <unistd.h>
46
+ #include <string.h>
47
+ #include <limits.h>
48
+ #include <sys/mman.h>
49
+ #include <errno.h>
50
+ #include <dlfcn.h>
51
+ #ifdef __sun
52
+ #include <sys/auxv.h>
53
+ #define ELF_TARGET_ALL
54
+ #endif /* __sun */
55
+ #ifdef __FreeBSD__
56
+ #include <sys/types.h>
57
+ #include <sys/user.h>
58
+ #include <libutil.h>
59
+ #endif
60
+ #include <elf.h>
61
+ #include <link.h>
62
+ #include "plthook.h"
63
+
64
+ #ifndef __GNUC__
65
+ #define __attribute__(arg)
66
+ #endif
67
+
68
+ #if defined __FreeBSD__ && defined __i386__ && __ELF_WORD_SIZE == 64
69
+ #error 32-bit application on 64-bit OS is not supported.
70
+ #endif
71
+
72
+ #if !defined(R_X86_64_JUMP_SLOT) && defined(R_X86_64_JMP_SLOT)
73
+ #define R_X86_64_JUMP_SLOT R_X86_64_JMP_SLOT
74
+ #endif
75
+
76
+ #if defined __x86_64__ || defined __x86_64
77
+ #define R_JUMP_SLOT R_X86_64_JUMP_SLOT
78
+ #define Elf_Plt_Rel Elf_Rela
79
+ #define PLT_DT_REL DT_RELA
80
+ #define R_GLOBAL_DATA R_X86_64_GLOB_DAT
81
+ #elif defined __i386__ || defined __i386
82
+ #define R_JUMP_SLOT R_386_JMP_SLOT
83
+ #define Elf_Plt_Rel Elf_Rel
84
+ #define PLT_DT_REL DT_REL
85
+ #define R_GLOBAL_DATA R_386_GLOB_DAT
86
+ #elif defined __arm__ || defined __arm
87
+ #define R_JUMP_SLOT R_ARM_JUMP_SLOT
88
+ #define Elf_Plt_Rel Elf_Rel
89
+ #elif defined __aarch64__ || defined __aarch64 /* ARM64 */
90
+ #define R_JUMP_SLOT R_AARCH64_JUMP_SLOT
91
+ #define Elf_Plt_Rel Elf_Rela
92
+ #elif defined __powerpc64__
93
+ #define R_JUMP_SLOT R_PPC64_JMP_SLOT
94
+ #define Elf_Plt_Rel Elf_Rela
95
+ #elif defined __powerpc__
96
+ #define R_JUMP_SLOT R_PPC_JMP_SLOT
97
+ #define Elf_Plt_Rel Elf_Rela
98
+ #elif 0 /* disabled because not tested */ && (defined __sparcv9 || defined __sparc_v9__)
99
+ #define R_JUMP_SLOT R_SPARC_JMP_SLOT
100
+ #define Elf_Plt_Rel Elf_Rela
101
+ #elif 0 /* disabled because not tested */ && (defined __sparc || defined __sparc__)
102
+ #define R_JUMP_SLOT R_SPARC_JMP_SLOT
103
+ #define Elf_Plt_Rel Elf_Rela
104
+ #elif 0 /* disabled because not tested */ && (defined __ia64 || defined __ia64__)
105
+ #define R_JUMP_SLOT R_IA64_IPLTMSB
106
+ #define Elf_Plt_Rel Elf_Rela
107
+ #else
108
+ #error unsupported OS
109
+ #endif
110
+
111
+ #if defined __LP64__
112
+ #ifndef ELF_CLASS
113
+ #define ELF_CLASS ELFCLASS64
114
+ #endif
115
+ #define SIZE_T_FMT "lu"
116
+ #define ELF_WORD_FMT "u"
117
+ #define ELF_XWORD_FMT "lu"
118
+ #define ELF_SXWORD_FMT "ld"
119
+ #define Elf_Half Elf64_Half
120
+ #define Elf_Xword Elf64_Xword
121
+ #define Elf_Sxword Elf64_Sxword
122
+ #define Elf_Ehdr Elf64_Ehdr
123
+ #define Elf_Phdr Elf64_Phdr
124
+ #define Elf_Sym Elf64_Sym
125
+ #define Elf_Dyn Elf64_Dyn
126
+ #define Elf_Rel Elf64_Rel
127
+ #define Elf_Rela Elf64_Rela
128
+ #ifndef ELF_R_SYM
129
+ #define ELF_R_SYM ELF64_R_SYM
130
+ #endif
131
+ #ifndef ELF_R_TYPE
132
+ #define ELF_R_TYPE ELF64_R_TYPE
133
+ #endif
134
+ #else /* __LP64__ */
135
+ #ifndef ELF_CLASS
136
+ #define ELF_CLASS ELFCLASS32
137
+ #endif
138
+ #define SIZE_T_FMT "u"
139
+ #ifdef __sun
140
+ #define ELF_WORD_FMT "lu"
141
+ #define ELF_XWORD_FMT "lu"
142
+ #define ELF_SXWORD_FMT "ld"
143
+ #else
144
+ #define ELF_WORD_FMT "u"
145
+ #define ELF_XWORD_FMT "u"
146
+ #define ELF_SXWORD_FMT "d"
147
+ #endif
148
+ #define Elf_Half Elf32_Half
149
+ #define Elf_Xword Elf32_Word
150
+ #define Elf_Sxword Elf32_Sword
151
+ #define Elf_Ehdr Elf32_Ehdr
152
+ #define Elf_Phdr Elf32_Phdr
153
+ #define Elf_Sym Elf32_Sym
154
+ #define Elf_Dyn Elf32_Dyn
155
+ #define Elf_Rel Elf32_Rel
156
+ #define Elf_Rela Elf32_Rela
157
+ #ifndef ELF_R_SYM
158
+ #define ELF_R_SYM ELF32_R_SYM
159
+ #endif
160
+ #ifndef ELF_R_TYPE
161
+ #define ELF_R_TYPE ELF32_R_TYPE
162
+ #endif
163
+ #endif /* __LP64__ */
164
+
165
+ #if defined(PT_GNU_RELRO) && !defined(__sun)
166
+ #define SUPPORT_RELRO /* RELRO (RELocation Read-Only) */
167
+ #if !defined(DF_1_NOW) && defined(DF_1_BIND_NOW)
168
+ #define DF_1_NOW DF_1_BIND_NOW
169
+ #endif
170
+ #endif
171
+
172
+ struct plthook {
173
+ const Elf_Sym *dynsym;
174
+ const char *dynstr;
175
+ size_t dynstr_size;
176
+ const char *plt_addr_base;
177
+ const Elf_Plt_Rel *plt;
178
+ size_t plt_cnt;
179
+ Elf_Xword r_type;
180
+ #ifdef SUPPORT_RELRO
181
+ const char *relro_start;
182
+ const char *relro_end;
183
+ #endif
184
+ };
185
+
186
+ static char errmsg[512];
187
+
188
+ #ifdef SUPPORT_RELRO
189
+ static size_t page_size;
190
+ #endif
191
+
192
+ static int plthook_open_executable(plthook_t **plthook_out);
193
+ static int plthook_open_shared_library(plthook_t **plthook_out, const char *filename);
194
+ static const Elf_Dyn *find_dyn_by_tag(const Elf_Dyn *dyn, Elf_Sxword tag);
195
+ #ifdef SUPPORT_RELRO
196
+ static int set_relro_members(plthook_t *plthook, struct link_map *lmap);
197
+ #endif
198
+ static int plthook_open_real(plthook_t **plthook_out, struct link_map *lmap);
199
+ static int check_elf_header(const Elf_Ehdr *ehdr);
200
+ static void set_errmsg(const char *fmt, ...) __attribute__((__format__ (__printf__, 1, 2)));
201
+
202
+ int plthook_open(plthook_t **plthook_out, const char *filename)
203
+ {
204
+ *plthook_out = NULL;
205
+ if (filename == NULL) {
206
+ return plthook_open_executable(plthook_out);
207
+ } else {
208
+ return plthook_open_shared_library(plthook_out, filename);
209
+ }
210
+ }
211
+
212
+ int plthook_open_by_handle(plthook_t **plthook_out, void *hndl)
213
+ {
214
+ struct link_map *lmap = NULL;
215
+
216
+ if (hndl == NULL) {
217
+ set_errmsg("NULL handle");
218
+ return PLTHOOK_FILE_NOT_FOUND;
219
+ }
220
+ if (dlinfo(hndl, RTLD_DI_LINKMAP, &lmap) != 0) {
221
+ set_errmsg("dlinfo error");
222
+ return PLTHOOK_FILE_NOT_FOUND;
223
+ }
224
+ return plthook_open_real(plthook_out, lmap);
225
+ }
226
+
227
+ int plthook_open_by_address(plthook_t **plthook_out, void *address)
228
+ {
229
+ #if defined __FreeBSD__
230
+ return PLTHOOK_NOT_IMPLEMENTED;
231
+ #else
232
+ Dl_info info;
233
+ struct link_map *lmap = NULL;
234
+
235
+ *plthook_out = NULL;
236
+ if (dladdr1(address, &info, (void**)&lmap, RTLD_DL_LINKMAP) == 0) {
237
+ set_errmsg("dladdr error");
238
+ return PLTHOOK_FILE_NOT_FOUND;
239
+ }
240
+ return plthook_open_real(plthook_out, lmap);
241
+ #endif
242
+ }
243
+
244
+ static int plthook_open_executable(plthook_t **plthook_out)
245
+ {
246
+ #if defined __linux__
247
+ return plthook_open_real(plthook_out, _r_debug.r_map);
248
+ #elif defined __sun
249
+ const char *auxv_file = "/proc/self/auxv";
250
+ #define NUM_AUXV_CNT 10
251
+ FILE *fp = fopen(auxv_file, "r");
252
+ auxv_t auxv;
253
+ struct r_debug *r_debug = NULL;
254
+
255
+ if (fp == NULL) {
256
+ set_errmsg("Could not open %s: %s", auxv_file,
257
+ strerror(errno));
258
+ return PLTHOOK_INTERNAL_ERROR;
259
+ }
260
+ while (fread(&auxv, sizeof(auxv_t), 1, fp) == 1) {
261
+ if (auxv.a_type == AT_SUN_LDDATA) {
262
+ r_debug = (struct r_debug *)auxv.a_un.a_ptr;
263
+ break;
264
+ }
265
+ }
266
+ fclose(fp);
267
+ if (r_debug == NULL) {
268
+ set_errmsg("Could not find r_debug");
269
+ return PLTHOOK_INTERNAL_ERROR;
270
+ }
271
+ return plthook_open_real(plthook_out, r_debug->r_map);
272
+ #elif defined __FreeBSD__
273
+ return plthook_open_shared_library(plthook_out, NULL);
274
+ #else
275
+ set_errmsg("Opening the main program is not supported on this platform.");
276
+ return PLTHOOK_NOT_IMPLEMENTED;
277
+ #endif
278
+ }
279
+
280
+ static int plthook_open_shared_library(plthook_t **plthook_out, const char *filename)
281
+ {
282
+ void *hndl = dlopen(filename, RTLD_LAZY | RTLD_NOLOAD);
283
+ struct link_map *lmap = NULL;
284
+
285
+ if (hndl == NULL) {
286
+ set_errmsg("dlopen error: %s", dlerror());
287
+ return PLTHOOK_FILE_NOT_FOUND;
288
+ }
289
+ if (dlinfo(hndl, RTLD_DI_LINKMAP, &lmap) != 0) {
290
+ set_errmsg("dlinfo error");
291
+ dlclose(hndl);
292
+ return PLTHOOK_FILE_NOT_FOUND;
293
+ }
294
+ dlclose(hndl);
295
+ return plthook_open_real(plthook_out, lmap);
296
+ }
297
+
298
+ static const Elf_Dyn *find_dyn_by_tag(const Elf_Dyn *dyn, Elf_Sxword tag)
299
+ {
300
+ while (dyn->d_tag != DT_NULL) {
301
+ if (dyn->d_tag == tag) {
302
+ return dyn;
303
+ }
304
+ dyn++;
305
+ }
306
+ return NULL;
307
+ }
308
+
309
+ #ifdef SUPPORT_RELRO
310
+ #if defined __linux__
311
+ static const char *get_mapped_file(const void *address, char *buf, int *err)
312
+ {
313
+ unsigned long addr = (unsigned long)address;
314
+ FILE *fp;
315
+
316
+ fp = fopen("/proc/self/maps", "r");
317
+ if (fp == NULL) {
318
+ set_errmsg("failed to open /proc/self/maps");
319
+ *err = PLTHOOK_INTERNAL_ERROR;
320
+ return NULL;
321
+ }
322
+ while (fgets(buf, PATH_MAX, fp) != NULL) {
323
+ unsigned long start, end;
324
+ int offset = 0;
325
+
326
+ sscanf(buf, "%lx-%lx %*s %*x %*x:%*x %*u %n", &start, &end, &offset);
327
+ if (offset == 0) {
328
+ continue;
329
+ }
330
+ if (start < addr && addr < end) {
331
+ char *p = buf + offset;
332
+ while (*p == ' ') {
333
+ p++;
334
+ }
335
+ if (*p != '/') {
336
+ continue;
337
+ }
338
+ p[strlen(p) - 1] = '\0'; /* remove '\n' */
339
+ fclose(fp);
340
+ return p;
341
+ }
342
+ }
343
+ fclose(fp);
344
+ set_errmsg("Could not find a mapped file reagion containing %p", address);
345
+ *err = PLTHOOK_INTERNAL_ERROR;
346
+ return NULL;
347
+ }
348
+ #elif defined __FreeBSD__
349
+ static const char *get_mapped_file(const void *address, char *buf, int *err)
350
+ {
351
+ uint64_t addr = (uint64_t)address;
352
+ struct kinfo_vmentry *top;
353
+ int i, cnt;
354
+
355
+ top = kinfo_getvmmap(getpid(), &cnt);
356
+ if (top == NULL) {
357
+ fprintf(stderr, "failed to call kinfo_getvmmap()\n");
358
+ *err = PLTHOOK_INTERNAL_ERROR;
359
+ return NULL;
360
+ }
361
+ for (i = 0; i < cnt; i++) {
362
+ struct kinfo_vmentry *kve = top + i;
363
+
364
+ if (kve->kve_start < addr && addr < kve->kve_end) {
365
+ strncpy(buf, kve->kve_path, PATH_MAX);
366
+ free(top);
367
+ return buf;
368
+ }
369
+ }
370
+ free(top);
371
+ set_errmsg("Could not find a mapped file reagion containing %p", address);
372
+ *err = PLTHOOK_INTERNAL_ERROR;
373
+ return NULL;
374
+ }
375
+ #else
376
+ static const char *get_mapped_file(const void *address, char *buf, int *err)
377
+ {
378
+ set_errmsg("Could not find a mapped file reagion containing %p", address);
379
+ *err = PLTHOOK_INTERNAL_ERROR;
380
+ return NULL;
381
+ }
382
+ #endif
383
+
384
+ static int set_relro_members(plthook_t *plthook, struct link_map *lmap)
385
+ {
386
+ char fnamebuf[PATH_MAX];
387
+ const char *fname;
388
+ FILE *fp;
389
+ Elf_Ehdr ehdr;
390
+ Elf_Half idx;
391
+ int rv;
392
+
393
+ if (lmap->l_name[0] == '/') {
394
+ fname = lmap->l_name;
395
+ } else {
396
+ int err;
397
+
398
+ fname = get_mapped_file(plthook->dynstr, fnamebuf, &err);
399
+ if (fname == NULL) {
400
+ return err;
401
+ }
402
+ }
403
+ fp = fopen(fname, "r");
404
+ if (fp == NULL) {
405
+ set_errmsg("failed to open %s", fname);
406
+ return PLTHOOK_INTERNAL_ERROR;
407
+ }
408
+ if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1) {
409
+ set_errmsg("failed to read the ELF header.");
410
+ fclose(fp);
411
+ return PLTHOOK_INVALID_FILE_FORMAT;
412
+ }
413
+ rv = check_elf_header(&ehdr);
414
+ if (rv != 0) {
415
+ fclose(fp);
416
+ return rv;
417
+ }
418
+
419
+ fseek(fp, ehdr.e_phoff, SEEK_SET);
420
+
421
+ for (idx = 0; idx < ehdr.e_phnum; idx++) {
422
+ Elf_Phdr phdr;
423
+
424
+ if (fread(&phdr, sizeof(phdr), 1, fp) != 1) {
425
+ set_errmsg("failed to read the program header table.");
426
+ fclose(fp);
427
+ return PLTHOOK_INVALID_FILE_FORMAT;
428
+ }
429
+ if (phdr.p_type == PT_GNU_RELRO) {
430
+ plthook->relro_start = plthook->plt_addr_base + phdr.p_vaddr;
431
+ plthook->relro_end = plthook->relro_start + phdr.p_memsz;
432
+ break;
433
+ }
434
+ }
435
+ fclose(fp);
436
+ return 0;
437
+ }
438
+ #endif
439
+
440
+ static int plthook_open_real(plthook_t **plthook_out, struct link_map *lmap)
441
+ {
442
+ plthook_t plthook = {NULL,};
443
+ const Elf_Dyn *dyn;
444
+ const char *dyn_addr_base = NULL;
445
+
446
+ #if defined __linux__
447
+ plthook.plt_addr_base = (char*)lmap->l_addr;
448
+ #elif defined __FreeBSD__ || defined __sun
449
+ const Elf_Ehdr *ehdr = (const Elf_Ehdr*)lmap->l_addr;
450
+ int rv = check_elf_header(ehdr);
451
+ if (rv != 0) {
452
+ return rv;
453
+ }
454
+ if (ehdr->e_type == ET_DYN) {
455
+ dyn_addr_base = (const char*)lmap->l_addr;
456
+ plthook.plt_addr_base = (const char*)lmap->l_addr;
457
+ }
458
+ #else
459
+ #error unsupported OS
460
+ #endif
461
+
462
+ /* get .dynsym section */
463
+ dyn = find_dyn_by_tag(lmap->l_ld, DT_SYMTAB);
464
+ if (dyn == NULL) {
465
+ set_errmsg("failed to find DT_SYMTAB");
466
+ return PLTHOOK_INTERNAL_ERROR;
467
+ }
468
+ plthook.dynsym = (const Elf_Sym*)(dyn_addr_base + dyn->d_un.d_ptr);
469
+
470
+ /* Check sizeof(Elf_Sym) */
471
+ dyn = find_dyn_by_tag(lmap->l_ld, DT_SYMENT);
472
+ if (dyn == NULL) {
473
+ set_errmsg("failed to find DT_SYMTAB");
474
+ return PLTHOOK_INTERNAL_ERROR;
475
+ }
476
+ if (dyn->d_un.d_val != sizeof(Elf_Sym)) {
477
+ set_errmsg("DT_SYMENT size %" ELF_XWORD_FMT " != %" SIZE_T_FMT, dyn->d_un.d_val, sizeof(Elf_Sym));
478
+ return PLTHOOK_INTERNAL_ERROR;
479
+ }
480
+
481
+ /* get .dynstr section */
482
+ dyn = find_dyn_by_tag(lmap->l_ld, DT_STRTAB);
483
+ if (dyn == NULL) {
484
+ set_errmsg("failed to find DT_STRTAB");
485
+ return PLTHOOK_INTERNAL_ERROR;
486
+ }
487
+ plthook.dynstr = dyn_addr_base + dyn->d_un.d_ptr;
488
+
489
+ /* get .dynstr size */
490
+ dyn = find_dyn_by_tag(lmap->l_ld, DT_STRSZ);
491
+ if (dyn == NULL) {
492
+ set_errmsg("failed to find DT_STRSZ");
493
+ return PLTHOOK_INTERNAL_ERROR;
494
+ }
495
+ plthook.dynstr_size = dyn->d_un.d_val;
496
+
497
+ /* get .rela.plt or .rel.plt section */
498
+ dyn = find_dyn_by_tag(lmap->l_ld, DT_JMPREL);
499
+ plthook.r_type = R_JUMP_SLOT;
500
+ #ifdef PLT_DT_REL
501
+ if (dyn == NULL) {
502
+ /* get .rela.dyn or .rel.dyn section */
503
+ dyn = find_dyn_by_tag(lmap->l_ld, PLT_DT_REL);
504
+ plthook.r_type = R_GLOBAL_DATA;
505
+ }
506
+ #endif
507
+ if (dyn == NULL) {
508
+ set_errmsg("failed to find DT_JMPREL");
509
+ return PLTHOOK_INTERNAL_ERROR;
510
+ }
511
+ plthook.plt = (const Elf_Plt_Rel *)(dyn_addr_base + dyn->d_un.d_ptr);
512
+
513
+ if (plthook.r_type == R_JUMP_SLOT) {
514
+ /* get total size of .rela.plt or .rel.plt */
515
+ dyn = find_dyn_by_tag(lmap->l_ld, DT_PLTRELSZ);
516
+ if (dyn == NULL) {
517
+ set_errmsg("failed to find DT_PLTRELSZ");
518
+ return PLTHOOK_INTERNAL_ERROR;
519
+ }
520
+
521
+ plthook.plt_cnt = dyn->d_un.d_val / sizeof(Elf_Plt_Rel);
522
+ #ifdef PLT_DT_REL
523
+ } else {
524
+ int total_size_tag = PLT_DT_REL == DT_RELA ? DT_RELASZ : DT_RELSZ;
525
+ int elem_size_tag = PLT_DT_REL == DT_RELA ? DT_RELAENT : DT_RELENT;
526
+ size_t total_size, elem_size;
527
+
528
+ dyn = find_dyn_by_tag(lmap->l_ld, total_size_tag);
529
+ if (dyn == NULL) {
530
+ set_errmsg("failed to find 0x%x", total_size_tag);
531
+ return PLTHOOK_INTERNAL_ERROR;
532
+ }
533
+ total_size = dyn->d_un.d_ptr;
534
+
535
+ dyn = find_dyn_by_tag(lmap->l_ld, elem_size_tag);
536
+ if (dyn == NULL) {
537
+ set_errmsg("failed to find 0x%x", elem_size_tag);
538
+ return PLTHOOK_INTERNAL_ERROR;
539
+ }
540
+ elem_size = dyn->d_un.d_ptr;
541
+ plthook.plt_cnt = total_size / elem_size;
542
+ #endif
543
+ }
544
+
545
+ #ifdef SUPPORT_RELRO
546
+ dyn = find_dyn_by_tag(lmap->l_ld, DT_FLAGS_1);
547
+ if (dyn != NULL && (dyn->d_un.d_val & DF_1_NOW)) {
548
+ int rv = set_relro_members(&plthook, lmap);
549
+ if (rv != 0) {
550
+ return rv;
551
+ }
552
+ if (page_size == 0) {
553
+ page_size = sysconf(_SC_PAGESIZE);
554
+ }
555
+ }
556
+ #endif
557
+
558
+ *plthook_out = malloc(sizeof(plthook_t));
559
+ if (*plthook_out == NULL) {
560
+ set_errmsg("failed to allocate memory: %" SIZE_T_FMT " bytes", sizeof(plthook_t));
561
+ return PLTHOOK_OUT_OF_MEMORY;
562
+ }
563
+ **plthook_out = plthook;
564
+ return 0;
565
+ }
566
+
567
+ static int check_elf_header(const Elf_Ehdr *ehdr)
568
+ {
569
+ static const unsigned short s = 1;
570
+ /* Check endianness at runtime. */
571
+ unsigned char elfdata = (*(const char*)&s) ? ELFDATA2LSB : ELFDATA2MSB;
572
+
573
+ if (ehdr == NULL) {
574
+ set_errmsg("invalid elf header address: NULL");
575
+ return PLTHOOK_INTERNAL_ERROR;
576
+ }
577
+
578
+ if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) != 0) {
579
+ set_errmsg("invalid file signature: 0x%02x,0x%02x,0x%02x,0x%02x",
580
+ ehdr->e_ident[0], ehdr->e_ident[1], ehdr->e_ident[2], ehdr->e_ident[3]);
581
+ return PLTHOOK_INVALID_FILE_FORMAT;
582
+ }
583
+ if (ehdr->e_ident[EI_CLASS] != ELF_CLASS) {
584
+ set_errmsg("invalid elf class: 0x%02x", ehdr->e_ident[EI_CLASS]);
585
+ return PLTHOOK_INVALID_FILE_FORMAT;
586
+ }
587
+ if (ehdr->e_ident[EI_DATA] != elfdata) {
588
+ set_errmsg("invalid elf data: 0x%02x", ehdr->e_ident[EI_DATA]);
589
+ return PLTHOOK_INVALID_FILE_FORMAT;
590
+ }
591
+ if (ehdr->e_ident[EI_VERSION] != EV_CURRENT) {
592
+ set_errmsg("invalid elf version: 0x%02x", ehdr->e_ident[EI_VERSION]);
593
+ return PLTHOOK_INVALID_FILE_FORMAT;
594
+ }
595
+ if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) {
596
+ set_errmsg("invalid file type: 0x%04x", ehdr->e_type);
597
+ return PLTHOOK_INVALID_FILE_FORMAT;
598
+ }
599
+ if (ehdr->e_version != EV_CURRENT) {
600
+ set_errmsg("invalid object file version: %" ELF_WORD_FMT, ehdr->e_version);
601
+ return PLTHOOK_INVALID_FILE_FORMAT;
602
+ }
603
+ if (ehdr->e_ehsize != sizeof(Elf_Ehdr)) {
604
+ set_errmsg("invalid elf header size: %u", ehdr->e_ehsize);
605
+ return PLTHOOK_INVALID_FILE_FORMAT;
606
+ }
607
+ if (ehdr->e_phentsize != sizeof(Elf_Phdr)) {
608
+ set_errmsg("invalid program header table entry size: %u", ehdr->e_phentsize);
609
+ return PLTHOOK_INVALID_FILE_FORMAT;
610
+ }
611
+ return 0;
612
+ }
613
+
614
+ int plthook_enum(plthook_t *plthook, unsigned int *pos, const char **name_out, void ***addr_out)
615
+ {
616
+ while (*pos < plthook->plt_cnt) {
617
+ const Elf_Plt_Rel *plt = plthook->plt + *pos;
618
+ if (ELF_R_TYPE(plt->r_info) == plthook->r_type) {
619
+ size_t idx = ELF_R_SYM(plt->r_info);
620
+
621
+ idx = plthook->dynsym[idx].st_name;
622
+ if (idx + 1 > plthook->dynstr_size) {
623
+ set_errmsg("too big section header string table index: %" SIZE_T_FMT, idx);
624
+ return PLTHOOK_INVALID_FILE_FORMAT;
625
+ }
626
+ *name_out = plthook->dynstr + idx;
627
+ *addr_out = (void**)(plthook->plt_addr_base + plt->r_offset);
628
+ (*pos)++;
629
+ return 0;
630
+ }
631
+ (*pos)++;
632
+ }
633
+ *name_out = NULL;
634
+ *addr_out = NULL;
635
+ return EOF;
636
+ }
637
+
638
+ int plthook_replace(plthook_t *plthook, const char *funcname, void *funcaddr, void **oldfunc)
639
+ {
640
+ size_t funcnamelen = strlen(funcname);
641
+ unsigned int pos = 0;
642
+ const char *name;
643
+ void **addr;
644
+ int rv;
645
+
646
+ if (plthook == NULL) {
647
+ set_errmsg("invalid argument: The first argument is null.");
648
+ return PLTHOOK_INVALID_ARGUMENT;
649
+ }
650
+ while ((rv = plthook_enum(plthook, &pos, &name, &addr)) == 0) {
651
+ if (strncmp(name, funcname, funcnamelen) == 0) {
652
+ if (name[funcnamelen] == '\0' || name[funcnamelen] == '@') {
653
+ #ifdef SUPPORT_RELRO
654
+ void *maddr = NULL;
655
+ if (plthook->relro_start <= (char*)addr && (char*)addr < plthook->relro_end) {
656
+ maddr = (void*)((size_t)addr & ~(page_size - 1));
657
+ if (mprotect(maddr, page_size, PROT_READ | PROT_WRITE) != 0) {
658
+ set_errmsg("Could not change the process memory protection at %p: %s",
659
+ maddr, strerror(errno));
660
+ return PLTHOOK_INTERNAL_ERROR;
661
+ }
662
+ }
663
+ #endif
664
+ if (oldfunc) {
665
+ *oldfunc = *addr;
666
+ }
667
+ *addr = funcaddr;
668
+ #ifdef SUPPORT_RELRO
669
+ if (maddr != NULL) {
670
+ mprotect(maddr, page_size, PROT_READ);
671
+ }
672
+ #endif
673
+ return 0;
674
+ }
675
+ }
676
+ }
677
+ if (rv == EOF) {
678
+ set_errmsg("no such function: %s", funcname);
679
+ rv = PLTHOOK_FUNCTION_NOT_FOUND;
680
+ }
681
+ return rv;
682
+ }
683
+
684
+ void plthook_close(plthook_t *plthook)
685
+ {
686
+ if (plthook != NULL) {
687
+ free(plthook);
688
+ }
689
+ }
690
+
691
+ const char *plthook_error(void)
692
+ {
693
+ return errmsg;
694
+ }
695
+
696
+ static void set_errmsg(const char *fmt, ...)
697
+ {
698
+ va_list ap;
699
+ va_start(ap, fmt);
700
+ vsnprintf(errmsg, sizeof(errmsg) - 1, fmt, ap);
701
+ va_end(ap);
702
+ }