-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path25-linker.lua
More file actions
386 lines (365 loc) · 16 KB
/
Copy path25-linker.lua
File metadata and controls
386 lines (365 loc) · 16 KB
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
local function trim(s) return string.match(s, '^()%s*$') and '' or string.match(s, '^%s*(.*%S)') end
local expect, do_syscall = expect, do_syscall
local function ar_load(data)
local pos = 1
local function read(c)
if pos > #data then return nil end
c = c or 1
local s = data:sub(pos, pos + c - 1)
pos = pos + c
return s
end
if read(8) ~= "!<arch>\n" then error("Not an ar archive", 2) end
local retval = {}
local name_table = nil
local name_rep = {}
while true do
local data = {}
local first_c = read()
while first_c == "\n" do first_c = read() end
if first_c == nil then break end
local name = read(15)
if name == nil then break end
name = first_c .. name
if string.find(name, "/") and string.find(name, "/") > 1 then name = string.sub(name, 1, string.find(name, "/") - 1)
else name = trim(name) end
data.timestamp = tonumber(trim(read(12)))
data.owner = tonumber(trim(read(6)))
data.group = tonumber(trim(read(6)))
data.mode = tonumber(trim(read(8)), 8)
local size = tonumber(trim(read(10)))
if read(2) ~= "`\n" then error("Invalid header for file " .. name, 2) end
if string.match(name, "^#1/%d+$") then name = read(tonumber(string.match(name, "#1/(%d+)")))
elseif string.match(name, "^/%d+$") then if name_table then
local n = tonumber(string.match(name, "/(%d+)"))
name = name_table:match("[^/]*", n+1)
else table.insert(name_rep, name) end end
data.name = name
data.data = read(size)
if name == "//" then name_table = data.data
elseif name ~= "/" and name ~= "/SYM64/" then table.insert(retval, data) end
end
if name_table then for k,v in pairs(name_rep) do
local n = tonumber(string.match(v, "/(%d+)"))
for l,w in pairs(retval) do if w.name == v then w.name = name_table:match("[^/]*", n+1) break end end
end end
local retval2 = {}
for _, v in ipairs(retval) do retval2[v.name] = v end
return retval2
end
--- Creates a new `package` and `require` set in a global table for the specified process.
-- @tparam Process process The process to make the functions for
-- @tparam _G G The global environment to install in
function createRequire(process, G)
local loaded, preload = {}, {}
G.package = {}
local oldenv = processes[process.parent] and processes[process.parent].env
if oldenv then
G.package.path = oldenv.package and oldenv.package.path
G.package.libpath = oldenv.package and oldenv.package.libpath
end
G.package.path = G.package.path or "/lib/?.lua;/lib/?/init.lua;/usr/lib/?.lua;/usr/lib/?/init.lua;./?.lua;./?/init.lua"
if type(process.vars.PACKAGE_PATH) == "string" then G.package.path = process.vars.PACKAGE_PATH .. ';' .. G.package.path end
G.package.libpath = G.package.libpath or "/lib/lib?.a;/usr/lib/lib?.a"
if type(process.vars.PACKAGE_LIBPATH) == "string" then G.package.libpath = process.vars.PACKAGE_LIBPATH .. ';' .. G.package.libpath end
G.package.config = "/\n;\n?\n!\n-"
G.package.loaded = loaded
G.package.preload = preload
G.package.forceload = false
for k, v in pairs(G) do if type(v) == "table" then loaded[k] = v end end
local sentinel = setmetatable({}, {__newindex = function() end, __metatable = false})
local localLoaders = {}
local function fileLoader(name, path)
local file, err = do_syscall("open", path, "rb")
if not file then error(path .. ": " .. err, 3) end
local data = file.readAll()
file.close()
local fn, err = load(data, "@" .. path, nil, _ENV)
if not fn then error(path .. ": " .. err, 3) end
local dir = path:match("^(.*)/[^/]*$") or "/"
localLoaders[#localLoaders+1] = function(name2)
local path, err = package.searchpath(name2, dir .. "/?.lua;" .. dir .. "/?/init.lua")
if not path then return nil, err end
return fileLoader, path
end
local ok, res = pcall(fn, name)
localLoaders[#localLoaders] = nil
if ok then return res
else error(path .. ": " .. res, 3) end
end
local function libraryLoader(name, path)
local libname
if path:find "%z" then path, libname = path:match "^([^%z]*)%z(.*)$"
elseif name:find "%-" then libname = name:match("^([^%-]*)%-(.*)$")
else libname = "init" end
local file, err = do_syscall("open", path, "rb")
if not file then error(path .. ": " .. err, 3) end
local data = file.readAll()
file.close()
local dir = ar_load(data)
local function preloader(name)
local p = name .. ".lua"
if not dir[p] then error(path .. ":" .. p .. ": File not found", 0) end
local data = dir[p].data
local fn, err = load(data, "@" .. path .. ":" .. p, nil, _ENV)
if not fn then error(path .. ":" .. p .. ": " .. err, 3) end
local ok, res = pcall(fn, name)
if ok then return res
else error(path .. ":" .. p .. ": " .. res) end
end
localLoaders[#localLoaders+1] = function(name2) return preloader, name2 end
local res = preloader(libname)
localLoaders[#localLoaders] = nil
return res
end
localLoaders[1] = function(name)
local dir = do_syscall("getname"):match("^(.*)/[^/]*$") or "/"
local path, err = package.searchpath(name, dir .. "/?.lua;" .. dir .. "/?/init.lua")
if not path then return nil, err end
return fileLoader, path
end
function G.package.searchpath(name, path, sep, rep)
expect(1, name, "string")
expect(2, path, "string")
expect(3, sep, "string", "nil")
expect(4, rep, "string", "nil")
sep = (sep or "."):gsub("([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
rep = (rep or "/"):gsub("([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
local msg = ""
for p in path:gmatch "[^;]+" do
local pp = p:gsub("%?", name:gsub(sep, rep), nil)
local file, err = do_syscall("open", pp, "r")
if file then
file.close()
return pp
else
msg = msg .. "\t" .. pp .. ": " .. err .. "\n"
end
end
return nil, msg
end
G.package.searchers = {
function(name)
local p = preload[name]
if p then return p else return nil, "\tpackage.preload['" .. name .. "']: No such field\n" end
end,
function(name)
local path, err = package.searchpath(name, package.path)
if not path then return nil, err end
return fileLoader, path, path
end,
function(name)
local path, err = package.searchpath(name:match("^[^%-]*"), package.libpath)
if not path then return nil, err end
local n = name:match("%-(.+)$")
if n then return libraryLoader, path, path .. ":" .. n
else return libraryLoader, path, path .. ":init" end
end,
function(name)
if not name:find "%." then return nil end
local path, err = package.searchpath(name:match("^[^%.]*"), package.libpath)
if not path then return nil, err end
local n = name:match("^[^%.]*%.(.*)$")
return libraryLoader, path .. "\0" .. n, path .. ":" .. n
end,
function(name)
if #localLoaders > 0 then return localLoaders[#localLoaders](name) end
return nil, "no local loaders found"
end
}
setfenv(fileLoader, G) debug.protect(fileLoader)
setfenv(libraryLoader, G) debug.protect(libraryLoader)
setfenv(localLoaders[1], G) debug.protect(localLoaders[1])
for _, v in pairs(G.package.searchers) do setfenv(v, G) debug.protect(v) end
function G.require(name)
expect(1, name, "string")
local err = "module '" .. name .. "' not found:\n"
local loader, arg, pathname
for _, v in ipairs(package.searchers) do
loader, arg, pathname = v(name)
if loader then break end
err = err .. (arg or "")
end
if not loader then error(err, 2) end
if pathname then
if loaded[pathname] then
if loaded[pathname] == sentinel then error("loop detected loading '" .. name .. "'", 3)
elseif not package.forceload then return loaded[pathname] end
end
loaded[pathname] = sentinel
else
if loaded[name] then
if loaded[name] == sentinel then error("loop detected loading '" .. name .. "'", 3)
elseif not package.forceload then return loaded[name] end
end
end
loaded[name] = sentinel
local ok, res = pcall(loader, name, arg)
if ok then
if res ~= nil then loaded[name] = res
elseif loaded[name] == sentinel then loaded[name] = true end
if pathname then
if res ~= nil then loaded[pathname] = res
elseif loaded[pathname] == sentinel then loaded[pathname] = true end
end
return loaded[name]
else
loaded[name] = nil
if pathname then loaded[pathname] = nil end
error(err .. "\t" .. res .. "\n", 2)
end
end
return G
end
-- Copy of require for the kernel
do
local loaded, preload = {}, {}
package = {}
package.path = "/lib/?.lua;/lib/?/init.lua;/usr/lib/?.lua;/usr/lib/?/init.lua"
package.libpath = "/lib/lib?.a;/usr/lib/lib?.a"
package.config = "/\n;\n?\n!\n-"
package.loaded = loaded
package.preload = preload
package.forceload = false
for k, v in pairs(_G) do if type(v) == "table" then loaded[k] = v end end
local sentinel = setmetatable({}, {__newindex = function() end, __metatable = false})
local localLoaders = {}
local function fileLoader(name, path)
local stat, err = filesystem.stat(KERNEL, path)
if not stat then error(path .. ": " .. err, 3) end
if stat.owner ~= "root" or stat.worldPermissions.write then error(path .. ": Insecure file permissions", 3) end
for k, v in pairs(stat.permissions) do if k ~= "root" and v.write then error(path .. ": Insecure file permissions", 3) end end
local file, err = filesystem.open(KERNEL, path, "rb")
if not file then error(path .. ": " .. err, 3) end
local data = file.readAll()
file.close()
local fn, err = load(data, "@" .. path, nil, _G)
if not fn then error(path .. ": " .. err, 3) end
local dir = path:match("^(.*)/[^/]*$") or "/"
localLoaders[#localLoaders+1] = function(name2)
local path, err = package.searchpath(name2, dir .. "/?.lua;" .. dir .. "/?/init.lua")
if not path then return nil, err end
return fileLoader, path
end
local ok, res = pcall(fn, name)
localLoaders[#localLoaders] = nil
if ok then return res
else error(path .. ": " .. res, 3) end
end
local function libraryLoader(name, path)
local libname
if path:find "%z" then path, libname = path:match "^([^%z]*)%z(.*)$"
elseif name:find "%-" then libname = name:match("^([^%-]*)%-(.*)$")
else libname = "init" end
local stat, err = filesystem.stat(KERNEL, path)
if not stat then error(path .. ": " .. err, 3) end
if stat.owner ~= "root" or stat.worldPermissions.write then error(path .. ": Insecure file permissions", 3) end
for k, v in pairs(stat.permissions) do if k ~= "root" and v.write then error(path .. ": Insecure file permissions", 3) end end
local file, err = filesystem.open(KERNEL, path, "rb")
if not file then error(path .. ": " .. err, 3) end
local data = file.readAll()
file.close()
local dir = ar_load(data)
local function preloader(name)
local p = name .. ".lua"
if not dir[p] then error(path .. ":" .. p .. ": File not found", 0) end
local data = dir[p].data
local fn, err = load(data, "@" .. path .. ":" .. p, nil, _ENV)
if not fn then error(path .. ":" .. p .. ": " .. err, 3) end
local ok, res = pcall(fn, name)
if ok then return res
else error(path .. ":" .. p .. ": " .. res) end
end
localLoaders[#localLoaders+1] = function(name2) return preloader, name2 end
local res = preloader(libname)
localLoaders[#localLoaders] = nil
return res
end
function package.searchpath(name, path, sep, rep)
expect(1, name, "string")
expect(2, path, "string")
expect(3, sep, "string", "nil")
expect(4, rep, "string", "nil")
sep = (sep or "."):gsub("([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
rep = (rep or "/"):gsub("([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
local msg = ""
for p in path:gmatch "[^;]+" do
local pp = p:gsub("%?", name:gsub(sep, rep), nil)
local file, err = filesystem.open(KERNEL, pp, "r")
if file then
file.close()
return pp
else
msg = msg .. "\t" .. pp .. ": " .. err .. "\n"
end
end
return nil, msg
end
package.searchers = {
function(name)
local p = preload[name]
if p then return p else return nil, "\tpackage.preload['" .. name .. "']: No such field\n" end
end,
function(name)
local path, err = package.searchpath(name, package.path)
if not path then return nil, err end
return fileLoader, path, path
end,
function(name)
local path, err = package.searchpath(name:match("^[^%-]*"), package.libpath)
if not path then return nil, err end
local n = name:match("%-(.+)$")
if n then return libraryLoader, path, path .. ":" .. n
else return libraryLoader, path, path .. ":init" end
end,
function(name)
if not name:find "%." then return nil end
local path, err = package.searchpath(name:match("^[^%.]*"), package.libpath)
if not path then return nil, err end
local n = name:match("^[^%.]*%.(.*)$")
return libraryLoader, path .. "\0" .. n, path .. ":" .. n
end,
function(name)
if #localLoaders > 0 then return localLoaders[#localLoaders](name) end
return nil, "\tno local loaders found"
end
}
function require(name)
expect(1, name, "string")
local err = "module '" .. name .. "' not found:\n"
local loader, arg, pathname
for _, v in ipairs(package.searchers) do
loader, arg, pathname = v(name)
if loader then break end
err = err .. (arg or "")
end
if not loader then error(err, 2) end
if pathname then
if loaded[pathname] then
if loaded[pathname] == sentinel then error("loop detected loading '" .. name .. "'", 3)
elseif not package.forceload then return loaded[pathname] end
end
loaded[pathname] = sentinel
else
if loaded[name] then
if loaded[name] == sentinel then error("loop detected loading '" .. name .. "'", 3)
elseif not package.forceload then return loaded[name] end
end
end
loaded[name] = sentinel
local ok, res = pcall(loader, name, arg)
if ok then
if res ~= nil then loaded[name] = res
elseif loaded[name] == sentinel then loaded[name] = true end
if pathname then
if res ~= nil then loaded[pathname] = res
elseif loaded[pathname] == sentinel then loaded[pathname] = true end
end
return loaded[name]
else
loaded[name] = nil
if pathname then loaded[pathname] = nil end
error(err .. "\t" .. res .. "\n", 2)
end
end
end