-
Notifications
You must be signed in to change notification settings - Fork 3
/
plthook_osx.c
executable file
·666 lines (608 loc) · 20.5 KB
/
plthook_osx.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
/* -*- indent-tabs-mode: nil -*-
*
* plthook_osx.c -- implementation of plthook for OS X
*
* URL: https://github.com/kubo/plthook
*
* ------------------------------------------------------
*
* Copyright 2014-2019 Kubo Takehiro <[email protected]>
* 2020 Geoffrey Horsington <[email protected]>
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of the authors.
*
*/
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <dlfcn.h>
#include <mach-o/dyld.h>
#include <mach-o/nlist.h>
#include <mach-o/fat.h>
#include <mach-o/swap.h>
#include "plthook.h"
#define PLTHOOK_DEBUG_CMD 1
#define PLTHOOK_DEBUG_BIND 1
#ifdef PLTHOOK_DEBUG_CMD
#define DEBUG_CMD(...) fprintf(stderr, __VA_ARGS__)
#else
#define DEBUG_CMD(...)
#endif
#ifdef PLTHOOK_DEBUG_BIND
#define DEBUG_BIND(...) fprintf(stderr, __VA_ARGS__)
#else
#define DEBUG_BIND(...)
#endif
#ifdef __LP64__
#define segment_command_ segment_command_64
#define section_ section_64
#define nlist_ nlist_64
#else
#define segment_command_ segment_command
#define section_ section
#define nlist_ nlist
#endif
typedef struct
{
const char *name;
void **addr;
} bind_address_t;
struct plthook
{
unsigned int num_entries;
char *strings;
bind_address_t entries[1];
};
#define MAX_SEGMENTS 8
typedef struct
{
plthook_t *plthook;
intptr_t slide;
int num_segments;
int linkedit_segment_idx;
int data_segment_idx;
void **lazy_symbols;
size_t lazy_symbols_size;
uint32_t lazy_symbols_isym_offset;
struct segment_command_ *segments[MAX_SEGMENTS];
} data_t;
static int plthook_open_real(plthook_t **plthook_out, uint32_t image_idx, const struct mach_header *mh, const char *image_name);
static void set_errmsg(const char *fmt, ...) __attribute__((__format__(__printf__, 1, 2)));
static char errmsg[512];
void *plthook_handle_by_name(const char *name)
{
void *mono_handle = NULL;
uint32_t cnt = _dyld_image_count();
for (uint32_t idx = 0; idx < cnt; idx++)
{
const char *image_name = idx ? _dyld_get_image_name(idx) : NULL;
if (image_name && strstr(image_name, name))
{
mono_handle = dlopen(image_name, RTLD_LAZY | RTLD_NOLOAD);
return mono_handle;
}
}
return NULL;
}
int plthook_open(plthook_t **plthook_out, const char *filename)
{
size_t namelen;
uint32_t cnt;
uint32_t idx;
if (filename == NULL)
{
return plthook_open_real(plthook_out, 0, NULL, NULL);
}
cnt = _dyld_image_count();
namelen = strlen(filename);
namelen = strlen(filename);
cnt = _dyld_image_count();
for (idx = 0; idx < cnt; idx++)
{
const char *image_name = _dyld_get_image_name(idx);
size_t offset = 0;
if (image_name == NULL)
{
*plthook_out = NULL;
set_errmsg("Cannot find file at image index %u", idx);
return PLTHOOK_INTERNAL_ERROR;
}
if (*filename != '/')
{
size_t image_name_len = strlen(image_name);
if (image_name_len > namelen)
{
offset = image_name_len - namelen;
}
}
if (strcmp(image_name + offset, filename) == 0)
{
return plthook_open_real(plthook_out, idx, NULL, image_name);
}
}
*plthook_out = NULL;
set_errmsg("Cannot find file: %s", filename);
return PLTHOOK_FILE_NOT_FOUND;
}
int plthook_open_by_handle(plthook_t **plthook_out, void *hndl)
{
int flags[] = {
RTLD_LAZY | RTLD_NOLOAD,
RTLD_LAZY | RTLD_NOLOAD | RTLD_FIRST,
};
int flag_idx;
uint32_t cnt = _dyld_image_count();
#define NUM_FLAGS (sizeof(flags) / sizeof(flags[0]))
if (hndl == NULL)
{
set_errmsg("NULL handle");
return PLTHOOK_FILE_NOT_FOUND;
}
for (flag_idx = 0; flag_idx < NUM_FLAGS; flag_idx++)
{
uint32_t idx;
for (idx = 0; idx < cnt; idx++)
{
const char *image_name = idx ? _dyld_get_image_name(idx) : NULL;
void *handle = dlopen(image_name, flags[flag_idx]);
if (handle != NULL)
{
dlclose(handle);
if (handle == hndl)
{
return plthook_open_real(plthook_out, idx, NULL, image_name);
}
}
}
}
set_errmsg("Cannot find the image correspond to handle %p", hndl);
return PLTHOOK_FILE_NOT_FOUND;
}
int plthook_open_by_address(plthook_t **plthook_out, void *address)
{
Dl_info dlinfo;
uint32_t idx = 0;
uint32_t cnt = _dyld_image_count();
if (!dladdr(address, &dlinfo))
{
*plthook_out = NULL;
set_errmsg("Cannot find address: %p", address);
return PLTHOOK_FILE_NOT_FOUND;
}
for (idx = 0; idx < cnt; idx++)
{
if (dlinfo.dli_fbase == _dyld_get_image_header(idx))
{
return plthook_open_real(plthook_out, idx, dlinfo.dli_fbase, dlinfo.dli_fname);
}
}
set_errmsg("Cannot find the image index for base address: %p", dlinfo.dli_fbase);
return PLTHOOK_FILE_NOT_FOUND;
}
static int load_la_symbols(data_t *data)
{
struct segment_command_ *data_segment = data->segments[data->data_segment_idx];
struct section_ *data_sections = (struct section_ *)((size_t)data_segment + sizeof(struct segment_command_));
struct section_ *section;
uint32_t seg_i = 0;
for (seg_i = 0; seg_i < data_segment->nsects; seg_i++)
{
section = &data_sections[seg_i];
DEBUG_CMD("__DATA section %s\n", section->sectname);
if ((section->flags & S_LAZY_SYMBOL_POINTERS) == S_LAZY_SYMBOL_POINTERS)
{
DEBUG_CMD("Found lazy symbol pointers\n");
data->lazy_symbols = (void **)(section->addr + data->slide);
data->lazy_symbols_size = section->size;
data->lazy_symbols_isym_offset = section->reserved1;
return 0;
}
}
return PLTHOOK_INTERNAL_ERROR;
}
static int get_base_offset(FILE *obj_file)
{
uint32_t magic;
struct fat_header header;
struct fat_arch arch;
int is_fat, needs_swap;
size_t header_size = sizeof(struct fat_header);
size_t arch_size = sizeof(struct fat_arch);
off_t arch_offset = (off_t)header_size;
uint32_t arch_i = 0;
fseek(obj_file, 0, SEEK_SET);
fread(&magic, sizeof(uint32_t), 1, obj_file);
is_fat = magic == FAT_CIGAM || magic == FAT_MAGIC;
needs_swap = magic == FAT_CIGAM;
if (!is_fat)
return 0; // No FAT => use offsets as-is
DEBUG_CMD("Got FAT binary\n");
fseek(obj_file, 0, SEEK_SET);
fread(&header, sizeof(struct fat_header), 1, obj_file);
if (needs_swap)
swap_fat_header(&header, 0);
for (arch_i = 0; arch_i < header.nfat_arch; arch_i++)
{
fseek(obj_file, arch_offset + arch_i * arch_size, SEEK_SET);
fread(&arch, arch_size, 1, obj_file);
if (needs_swap)
swap_fat_arch(&arch, 1, 0);
switch (arch.cputype)
{
case CPU_TYPE_I386:
DEBUG_CMD("Has arch: I386, offset: %8x\n", arch.offset);
#ifndef __LP64__
return arch.offset;
#endif
break;
case CPU_TYPE_X86_64:
DEBUG_CMD("Has arch: X86_64, offset: %8x\n", arch.offset);
#if __LP64__
return arch.offset;
#endif
break;
default:
break;
}
}
return -1;
}
static int plthook_open_real(plthook_t **plthook_out, uint32_t image_idx, const struct mach_header *mh, const char *image_name)
{
struct load_command *cmd;
struct dysymtab_command *dysymtab_command;
struct symtab_command *symtab_command;
uint32_t lazy_bind_off = 0;
uint32_t lazy_bind_size = 0;
unsigned int nbind;
data_t data = {
NULL,
};
size_t size;
int i;
uint32_t symbol_index;
size_t num_funcs;
uint32_t *indirect_symbols;
struct nlist_ *symbols;
char *symbol_strings;
FILE *dfile;
int base_offset;
data.linkedit_segment_idx = -1;
data.data_segment_idx = -1;
data.slide = _dyld_get_image_vmaddr_slide(image_idx);
DEBUG_CMD("slide=%" PRIxPTR "\n", data.slide);
if (mh == NULL)
{
mh = _dyld_get_image_header(image_idx);
}
if (image_name == NULL)
{
image_name = _dyld_get_image_name(image_idx);
}
DEBUG_CMD("Image name: %s\n", image_name);
#ifdef __LP64__
cmd = (struct load_command *)((size_t)mh + sizeof(struct mach_header_64));
#else
cmd = (struct load_command *)((size_t)mh + sizeof(struct mach_header));
#endif
for (i = 0; i < mh->ncmds; i++)
{
struct dyld_info_command *dyld_info;
struct segment_command *segment;
struct segment_command_64 *segment64;
switch (cmd->cmd)
{
case LC_SEGMENT: /* 0x1 */
segment = (struct segment_command *)cmd;
DEBUG_CMD("LC_SEGMENT\n"
" segname %s\n"
" vmaddr %8x vmsize %8x\n"
" fileoff %8x filesize %8x\n"
" maxprot %8x initprot %8x\n"
" nsects %8d flags %8x\n",
segment->segname,
segment->vmaddr, segment->vmsize,
segment->fileoff, segment->filesize,
segment->maxprot, segment->initprot,
segment->nsects, segment->flags);
#ifndef __LP64__
if (strcmp(segment->segname, "__LINKEDIT") == 0)
{
data.linkedit_segment_idx = data.num_segments;
}
if (strcmp(segment->segname, "__DATA") == 0)
{
data.data_segment_idx = data.num_segments;
}
if (data.num_segments == MAX_SEGMENTS)
{
set_errmsg("Too many segments: %s", image_name);
return PLTHOOK_INTERNAL_ERROR;
}
data.segments[data.num_segments++] = segment;
#endif
break;
case LC_SEGMENT_64: /* 0x19 */
segment64 = (struct segment_command_64 *)cmd;
DEBUG_CMD("LC_SEGMENT_64\n"
" segname %s\n"
" vmaddr %8llx vmsize %8llx\n"
" fileoff %8llx filesize %8llx\n"
" maxprot %8x initprot %8x\n"
" nsects %8d flags %8x\n",
segment64->segname,
segment64->vmaddr, segment64->vmsize,
segment64->fileoff, segment64->filesize,
segment64->maxprot, segment64->initprot,
segment64->nsects, segment64->flags);
#ifdef __LP64__
if (strcmp(segment64->segname, "__LINKEDIT") == 0)
{
data.linkedit_segment_idx = data.num_segments;
}
if (strcmp(segment64->segname, "__DATA") == 0)
{
data.data_segment_idx = data.num_segments;
}
if (data.num_segments == MAX_SEGMENTS)
{
set_errmsg("Too many segments: %s", image_name);
return PLTHOOK_INTERNAL_ERROR;
}
data.segments[data.num_segments++] = segment64;
#endif
break;
case LC_DYLD_INFO_ONLY: /* (0x22|LC_REQ_DYLD) */
dyld_info = (struct dyld_info_command *)cmd;
lazy_bind_off = dyld_info->lazy_bind_off;
lazy_bind_size = dyld_info->lazy_bind_size;
DEBUG_CMD("LC_DYLD_INFO_ONLY\n"
" offset size\n"
" rebase %8x %8x\n"
" bind %8x %8x\n"
" weak_bind %8x %8x\n"
" lazy_bind %8x %8x\n"
" export_bind %8x %8x\n",
dyld_info->rebase_off, dyld_info->rebase_size,
dyld_info->bind_off, dyld_info->bind_size,
dyld_info->weak_bind_off, dyld_info->weak_bind_size,
dyld_info->lazy_bind_off, dyld_info->lazy_bind_size,
dyld_info->export_off, dyld_info->export_size);
break;
case LC_SYMTAB: /* 0x2 */
symtab_command = (struct symtab_command *)cmd;
DEBUG_CMD("LC_SYMTAB\n"
"symbol count: %8x\n"
"symbol offset: %8x\n"
"stf offset: %8x\n"
"str size: %8x\n",
symtab_command->nsyms,
symtab_command->symoff,
symtab_command->stroff,
symtab_command->strsize);
break;
case LC_DYSYMTAB: /* 0xb */
dysymtab_command = (struct dysymtab_command *)cmd;
DEBUG_CMD("LC_DYSYMTAB\n"
"itable offset: %8x\n"
"itable count: %8x\n",
dysymtab_command->indirectsymoff,
dysymtab_command->nindirectsyms);
break;
case LC_LOAD_DYLIB: /* 0xc */
DEBUG_CMD("LC_LOAD_DYLIB\n");
break;
case LC_ID_DYLIB: /* 0xd */
DEBUG_CMD("LC_ID_DYLIB\n");
break;
case LC_LOAD_DYLINKER: /* 0xe */
DEBUG_CMD("LC_LOAD_DYLINKER\n");
break;
case LC_ROUTINES_64: /* 0x1a */
DEBUG_CMD("LC_ROUTINES_64\n");
break;
case LC_UUID: /* 0x1b */
DEBUG_CMD("LC_UUID\n");
break;
case LC_VERSION_MIN_MACOSX: /* 0x24 */
DEBUG_CMD("LC_VERSION_MIN_MACOSX\n");
break;
case LC_FUNCTION_STARTS: /* 0x26 */
DEBUG_CMD("LC_FUNCTION_STARTS\n");
break;
case LC_MAIN: /* 0x28|LC_REQ_DYLD */
DEBUG_CMD("LC_MAIN\n");
break;
case LC_DATA_IN_CODE: /* 0x29 */
DEBUG_CMD("LC_DATA_IN_CODE\n");
break;
case LC_SOURCE_VERSION: /* 0x2A */
DEBUG_CMD("LC_SOURCE_VERSION\n");
break;
case LC_DYLIB_CODE_SIGN_DRS: /* 0x2B */
DEBUG_CMD("LC_DYLIB_CODE_SIGN_DRS\n");
break;
default:
DEBUG_CMD("LC_? (0x%x)\n", cmd->cmd);
}
cmd = (struct load_command *)((size_t)cmd + cmd->cmdsize);
}
if (data.linkedit_segment_idx == -1)
{
set_errmsg("Cannot find the linkedit segment: %s", image_name);
return PLTHOOK_INVALID_FILE_FORMAT;
}
if (data.data_segment_idx == -1)
{
set_errmsg("Cannot find the data segment: %s", image_name);
return PLTHOOK_INVALID_FILE_FORMAT;
}
if (load_la_symbols(&data) != 0)
{
set_errmsg("Cannot locate lazily loaded symbols: %s", image_name);
return PLTHOOK_INVALID_FILE_FORMAT;
}
num_funcs = data.lazy_symbols_size / sizeof(void *);
size = offsetof(plthook_t, entries) + sizeof(bind_address_t) * num_funcs;
data.plthook = (plthook_t *)malloc(size);
if (data.plthook == NULL)
{
set_errmsg("failed to allocate memory: %" PRIuPTR " bytes", size);
return PLTHOOK_OUT_OF_MEMORY;
}
dfile = fopen(image_name, "r");
if (dfile == NULL)
{
set_errmsg("Failed to open image (no permissions or in use?): %s", image_name);
free(data.plthook);
return PLTHOOK_INVALID_FILE_FORMAT;
}
base_offset = get_base_offset(dfile);
if (base_offset < 0) {
set_errmsg("The binary is a FAT binary but has no binary for the current arch.");
free(data.plthook);
fclose(dfile);
return PLTHOOK_INVALID_FILE_FORMAT;
}
DEBUG_CMD("Got base offset: %x\n", base_offset);
indirect_symbols = malloc(sizeof(uint32_t) * dysymtab_command->nindirectsyms);
symbols = malloc(sizeof(struct nlist_) * symtab_command->nsyms);
symbol_strings = malloc(sizeof(char) * symtab_command->strsize);
fseek(dfile, base_offset + dysymtab_command->indirectsymoff, SEEK_SET);
fread(indirect_symbols, sizeof(uint32_t), dysymtab_command->nindirectsyms, dfile);
fseek(dfile, base_offset + symtab_command->symoff, SEEK_SET);
fread(symbols, sizeof(struct nlist_), symtab_command->nsyms, dfile);
fseek(dfile, base_offset + symtab_command->stroff, SEEK_SET);
fread(symbol_strings, sizeof(char), symtab_command->strsize, dfile);
fclose(dfile);
data.plthook->num_entries = num_funcs;
data.plthook->strings = symbol_strings;
for (i = 0; i < num_funcs; i++)
{
symbol_index = indirect_symbols[i + data.lazy_symbols_isym_offset];
DEBUG_CMD("Symbol (%p) %d -> %s\n", data.lazy_symbols[i], symbol_index, symbol_strings + symbols[symbol_index].n_un.n_strx);
data.plthook->entries[i].name = symbol_strings + symbols[symbol_index].n_un.n_strx;
data.plthook->entries[i].addr = &data.lazy_symbols[i];
}
free(indirect_symbols);
free(symbols);
*plthook_out = data.plthook;
return 0;
}
int plthook_enum(plthook_t *plthook, unsigned int *pos, const char **name_out, void ***addr_out)
{
if (*pos >= plthook->num_entries)
{
*name_out = NULL;
*addr_out = NULL;
return EOF;
}
*name_out = plthook->entries[*pos].name;
*addr_out = plthook->entries[*pos].addr;
(*pos)++;
return 0;
}
int plthook_replace(plthook_t *plthook, const char *funcname, void *funcaddr, void **oldfunc)
{
size_t funcnamelen = strlen(funcname);
unsigned int pos = 0;
const char *name;
void **addr;
int rv;
if (plthook == NULL)
{
set_errmsg("invalid argument: The first argument is null.");
return PLTHOOK_INVALID_ARGUMENT;
}
while ((rv = plthook_enum(plthook, &pos, &name, &addr)) == 0)
{
if (strncmp(name, funcname, funcnamelen) == 0)
{
if (name[funcnamelen] == '\0' || name[funcnamelen] == '$')
{
goto matched;
}
}
if (name[0] == '@')
{
/* Oracle libclntsh.dylib imports 'read' as '@_read'. */
name++;
if (strncmp(name, funcname, funcnamelen) == 0)
{
if (name[funcnamelen] == '\0' || name[funcnamelen] == '$')
{
goto matched;
}
}
}
if (name[0] == '_')
{
name++;
if (strncmp(name, funcname, funcnamelen) == 0)
{
if (name[funcnamelen] == '\0' || name[funcnamelen] == '$')
{
goto matched;
}
}
}
continue;
matched:
if (oldfunc)
{
*oldfunc = *addr;
}
*addr = funcaddr;
return 0;
}
if (rv == EOF)
{
set_errmsg("no such function: %s", funcname);
rv = PLTHOOK_FUNCTION_NOT_FOUND;
}
return rv;
}
void plthook_close(plthook_t *plthook)
{
if (plthook != NULL)
{
free(plthook->strings);
free(plthook);
}
return;
}
const char *plthook_error(void)
{
return errmsg;
}
static void set_errmsg(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vsnprintf(errmsg, sizeof(errmsg) - 1, fmt, ap);
va_end(ap);
}