-
Notifications
You must be signed in to change notification settings - Fork 3
/
extensions.c
173 lines (154 loc) · 6.46 KB
/
extensions.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
/*
* j2 - A simple concatenative programming language that can understand
* the structure of and dynamically link with compatible C binaries.
*
* Copyright (C) 2011 Jason Nyberg <[email protected]>
* Copyright (C) 2018 Jason Nyberg <[email protected]> (dual-licensed)
* (C) Copyright 2019 Hewlett Packard Enterprise Development LP.
*
* This file is part of j2.
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of either
*
* * the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at
* your option) any later version
*
* or
*
* * the GNU General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at
* your option) any later version
*
* or both in parallel, as here.
*
* j2 is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received copies of the GNU General Public License and
* the GNU Lesser General Public License along with this program. If
* not, see <http://www.gnu.org/licenses/>.
*/
//#define _GNU_SOURCE
#define _C99
#include <libelf.h>
#include <elfutils/libdwelf.h>
#include <unistd.h>
#include <fcntl.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dlfcn.h> // dlopen/dlsym/dlclose
#include "util.h"
#include "vm.h"
#include "extensions.h"
extern int square(int a) { return a*a; }
extern int minus(int a,int b) { return a-b; }
extern int string(char *s) { fprintf(outfile(),"%s\n",s); }
extern LTV *ltv_coersion_test(LTV *ltv) { print_ltv(outfile(),CODE_RED,ltv,CODE_RESET "\n",0); return ltv; } // no change to stack means success
extern FILE *get_stdin() { return stdin; }
extern FILE *get_stdout() { return stdout; }
extern FILE *get_stderr() { return stderr; }
extern LTV *brl(FILE *fp) {
int len; char *data=NULL;
return (data=balanced_readline(fp,"[","]",&len))?LTV_init(NEW(LTV),data,len,LT_OWN):NULL;
}
extern void vm_try(int exp) { if (exp) vm_throw(LTV_NULL); }
extern FILE *file_open(char *filename,char *opts) { return fopen(filename,opts); }
extern void file_close(FILE *fp) { fclose(fp); }
extern void pinglib(char *filename) {
void *dlhandle=dlopen(filename,RTLD_LAZY | RTLD_GLOBAL | RTLD_NODELETE | RTLD_DEEPBIND);
if (!dlhandle)
fprintf(outfile(),"dlopen error: handle %x %s\n",dlhandle,dlerror());
else
dlclose(dlhandle);
}
extern LTV *ltvenv(char *var) {
char *val=getenv(var);
char *buf=val;
int len=0;
if (val)
len=strlen(val);
for (int i=0;buf<(val+len);buf+=i+1) {
i=series(buf,len,NULL,":",NULL);
fstrnprint(outfile(),buf,i);
}
return LTV_init(NEW(LTV),val,-1,LT_DUP);
}
extern void printptr(char *var) { printf("%x\n",var); }
// while (access(fileName, R_OK ));
/*
So, for example, suppose you ask GDB to debug /usr/bin/ls, which has a debug link that specifies the file ls.debug, and a build ID whose value in hex is abcdef1234. If the list of the global debug directories includes /usr/lib/debug, then GDB will look for the following debug information files, in the indicated order:
- /usr/lib/debug/.build-id/ab/cdef1234.debug
- /usr/bin/ls.debug
- /usr/bin/.debug/ls.debug
- /usr/lib/debug/usr/bin/ls.debug.
*/
// compiled separately because of it's use of libdwelf, which conflicts with libdwarf IIRC
LTV *get_separated_debug_filename(char *filename)
{
LTV *debug_filename=NULL;
if ( elf_version ( EV_CURRENT ) != EV_NONE ) {
int fd=open(filename,O_RDONLY);
if (fd) {
Elf *elf=elf_begin(fd,ELF_C_READ,NULL);
if (elf) {
GElf_Word crc;
const void *buildid;
const char *debuglink=dwelf_elf_gnu_debuglink(elf,&crc);
if (debuglink) {
debug_filename=LTV_init(NEW(LTV),(char *) debuglink,-1,LT_DUP);
ssize_t idlen=dwelf_elf_gnu_build_id(elf,&buildid);
Dwarf *dwarf=dwarf_begin_elf(elf,DWARF_C_READ,NULL);
const char *altname;
if (dwarf) {
int idlen=dwelf_dwarf_gnu_debugaltlink(dwarf,&altname,&buildid);
dwarf_end(dwarf);
}
LTV *buildid_filename=LTV_init(NEW(LTV),mymalloc(256),256,LT_OWN);
char *buf=(char *) buildid_filename->data;
buf+=sprintf(buf,"/usr/lib/debug/.build-id/%02x",*((unsigned char *) buildid++));
buf+=sprintf(buf,"/");
while (--idlen)
buf+=sprintf(buf,"%02x",*((unsigned char *) buildid++));
buf+=sprintf(buf,".debug");
buildid_filename->len=buf-(char *) (buildid_filename->data);
LT_put(debug_filename,"buildid",TAIL,buildid_filename);
fprintf(outfile(),CODE_BLUE "debug filename candidates: %s %s" CODE_RESET "\n",debug_filename->data,buildid_filename->data);
}
elf_end(elf);
}
close(fd);
}
}
return debug_filename;
}
extern LTV *null() { return LTV_NULL; }
extern void is_null(LTV *tos) { if (!(tos->flags<_NULL)) vm_throw(LTV_NULL); }
extern void int_iszero(int a) { if (a) vm_throw(LTV_NULL); }
extern void int_iseq(int a,int b) { if (a!=b) vm_throw(LTV_NULL); }
extern void int_isneq(int a,int b) { if (a==b) vm_throw(LTV_NULL); }
extern void int_islt(int a,int b) { if (!(a<b)) vm_throw(LTV_NULL); }
extern void int_isgt(int a,int b) { if (!(a>b)) vm_throw(LTV_NULL); }
extern void int_islteq(int a,int b) { if (a>b) vm_throw(LTV_NULL); }
extern void int_isgteq(int a,int b) { if (a<b) vm_throw(LTV_NULL); }
extern int int_add(int a,int b) { return a+b; }
extern int int_mul(int a,int b) { return a*b; }
extern int int_inc(int a) { return ++a; }
extern int int_dec(int a) { return --a; }
extern void int_to_ascii(int i) { fprintf(outfile(),"%c\n",i); }
int benchint=0;
extern void bench() {
if (--benchint<0) {
fprintf(outfile()," done!\n");
benchint=0;
vm_throw(LTV_NULL);
}
return;
}
test_callback_sig callback_example=NULL;
extern int test_callback(int a,int b) { return callback_example? callback_example(a,b):0; }