aboutsummaryrefslogtreecommitdiff
path: root/user/lib/ld-weenix/ldutil.c
blob: b6509a3c394f9722fc5114b940a490e1a26bc162 (plain)
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
/*
 *  File: ldutil.c
 *  Date: 15 March 1998
 *  Acct: David Powell (dep)
 *  Desc: Functions that didn't fit anywhere else
 *
 *
 *  Acct: Sandy Harvie (charvie)
 *  Date: 27 March 2019
 *  Desc: Modified for x86-64
 */

/* LINTLIBRARY */

#include "fcntl.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "unistd.h"

#include "ldtypes.h"
#include "ldutil.h"

static const char *err_zero = "ld.so.1: panic - unable to open /dev/zero\n";

/* I wrote this back before I had printf... maybe it should disappear. */

void _ldverify(int test, const char *msg)
{
    if (test)
    {
        (void)write(STDERR_FILENO, msg, strlen(msg));
        exit(LD_ERR_EXIT);
    }
}

/* This function simply attempts to open /dev/zero, exiting if the call
 * to open failed.  The file descriptor of the newly opened file is
 * returned. */

int _ldzero()
{
    int zfd;

    if ((zfd = open("/dev/zero", O_RDONLY, 0)) < 0)
    {
        printf("%s", err_zero);
        exit(1);
    }

    return zfd;
}

/* This is the hash operation used for the string-to-symbol hash
 * table in dynamic ELF binaries.  This function is taken more or less
 * directly from the LLM */

unsigned long _ldelfhash(const char *name)
{
    uint32_t h = 0, g;

    while (*name)
    {
        h = (h << 4) + *name++;
        /* LINTED */
        if ((g = h & 0xf0000000))
            h ^= g >> 24;
        h &= ~g;
    }

    return h;
}