1 /* $OpenBSD: ksyms.c,v 1.8 2015/02/07 03:26:20 tedu Exp $ */
4 * Copyright (c) 2008 Miodrag Vallat.
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 #include <sys/elf64.h>
30 typedef Elf64_Ehdr Elf_Ehdr
;
31 typedef Elf64_Shdr Elf_Shdr
;
33 static int ksyms_elf_parse(void);
41 char symbuf
[1 + BUFSIZ
], *sym
, *end
;
44 for (tries
= 0; tries
< MAXBADWORDS
; tries
++) {
45 pos
= arc4random_uniform(symsize
);
46 if (lseek(symfd
, pos
+ symoffs
, SEEK_SET
) == -1)
48 buflen
= read(symfd
, symbuf
, BUFSIZ
);
53 * The buffer is hopefully large enough to hold at least
54 * a complete symbol, i.e. two occurences of NUL, or
55 * one occurence of NUL and the buffer containing the end
56 * of the string table. We make sure the buffer will be
57 * NUL terminated in all cases.
59 if (buflen
+ pos
>= symsize
)
60 buflen
= symsize
- pos
;
61 *(end
= symbuf
+ buflen
) = '\0';
63 for (sym
= symbuf
; *sym
!= '\0'; sym
++)
68 symlen
= strlen(++sym
);
69 if (symlen
< MINLEN
|| symlen
> MAXLEN
)
72 /* ignore symbols containing dots or dollar signs */
73 if (strchr(sym
, '.') != NULL
|| strchr(sym
, '$') != NULL
)
79 if (tries
>= MAXBADWORDS
) {
80 mvcur(0, COLS
- 1, LINES
-1, 0);
82 errx(1, "can't seem a suitable symbol in %s",
86 strlcpy(Word
, sym
, sizeof(Word
));
87 strlcpy(Known
, sym
, sizeof(Known
));
88 for (sym
= Known
; *sym
!= '\0'; sym
++) {
90 *sym
= '_'; /* try not to confuse player */
91 if (isalnum((unsigned char)*sym
))
99 if ((symfd
= open(Dict_name
, O_RDONLY
)) < 0)
102 if (ksyms_elf_parse() == 0)
111 ksyms_elf_parse(void)
117 if (lseek(symfd
, 0, SEEK_SET
) == -1)
120 if (read(symfd
, &eh
, sizeof(eh
)) != sizeof(eh
))
126 if (lseek(symfd
, eh
.e_shoff
, SEEK_SET
) == -1)
132 for (s
= 0; s
< eh
.e_shnum
; s
++) {
133 if (read(symfd
, &sh
, sizeof(sh
)) != sizeof(sh
))
137 * There should be two string table sections, one with the
138 * name of the sections themselves, and one with the symbol
139 * names. Just pick the largest one.
141 if (sh
.sh_type
== SHT_STRTAB
) {
142 if (symsize
> (off_t
)sh
.sh_size
)
145 symoffs
= (off_t
)sh
.sh_offset
;
146 symsize
= (off_t
)sh
.sh_size
;