Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
R
relibc
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Peter Limkilde Svendsen
relibc
Commits
a3f7a174
Verified
Commit
a3f7a174
authored
5 years ago
by
Jeremy Soller
Browse files
Options
Downloads
Patches
Plain Diff
WIP - implementation of dlfcn
parent
2cbc78f2
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Pipeline
#6723
failed
5 years ago
Stage: build
Stage: test
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/header/dlfcn/mod.rs
+66
-10
66 additions, 10 deletions
src/header/dlfcn/mod.rs
src/ld_so/linker.rs
+16
-8
16 additions, 8 deletions
src/ld_so/linker.rs
with
82 additions
and
18 deletions
src/header/dlfcn/mod.rs
+
66
−
10
View file @
a3f7a174
...
@@ -5,7 +5,7 @@ use core::{
...
@@ -5,7 +5,7 @@ use core::{
sync
::
atomic
::{
AtomicUsize
,
Ordering
},
sync
::
atomic
::{
AtomicUsize
,
Ordering
},
};
};
use
crate
::{
c_str
::
CStr
,
platform
::
types
::
*
};
use
crate
::{
c_str
::
CStr
,
ld_so
::
tcb
::
Tcb
,
platform
::
types
::
*
};
pub
const
RTLD_LAZY
:
c_int
=
0x0001
;
pub
const
RTLD_LAZY
:
c_int
=
0x0001
;
pub
const
RTLD_NOW
:
c_int
=
0x0002
;
pub
const
RTLD_NOW
:
c_int
=
0x0002
;
...
@@ -47,8 +47,41 @@ pub unsafe extern "C" fn dlopen(filename: *const c_char, flags: c_int) -> *mut c
...
@@ -47,8 +47,41 @@ pub unsafe extern "C" fn dlopen(filename: *const c_char, flags: c_int) -> *mut c
eprintln!
(
"dlopen({:?}, {:#>04x})"
,
filename_opt
,
flags
);
eprintln!
(
"dlopen({:?}, {:#>04x})"
,
filename_opt
,
flags
);
if
let
Some
(
filename
)
=
filename_opt
{
if
let
Some
(
filename
)
=
filename_opt
{
ERROR
.store
(
ERROR_NOT_SUPPORTED
.as_ptr
()
as
usize
,
Ordering
::
SeqCst
);
if
let
Some
(
tcb
)
=
Tcb
::
current
()
{
ptr
::
null_mut
()
if
tcb
.linker_ptr
.is_null
()
{
eprintln!
(
"dlopen: linker not found"
);
ERROR
.store
(
ERROR_NOT_SUPPORTED
.as_ptr
()
as
usize
,
Ordering
::
SeqCst
);
return
ptr
::
null_mut
();
}
eprintln!
(
"dlopen: linker_ptr: {:p}"
,
tcb
.linker_ptr
);
let
mut
linker
=
(
&*
tcb
.linker_ptr
)
.lock
();
match
linker
.load_library
(
filename
)
{
Ok
(())
=>
(),
Err
(
err
)
=>
{
eprintln!
(
"dlopen: failed to load {}"
,
filename
);
ERROR
.store
(
ERROR_NOT_SUPPORTED
.as_ptr
()
as
usize
,
Ordering
::
SeqCst
);
return
ptr
::
null_mut
();
}
}
match
linker
.link
(
None
)
{
Ok
(
ok
)
=>
(),
Err
(
err
)
=>
{
eprintln!
(
"dlopen: failed to link '{}': {}"
,
filename
,
err
);
ERROR
.store
(
ERROR_NOT_SUPPORTED
.as_ptr
()
as
usize
,
Ordering
::
SeqCst
);
return
ptr
::
null_mut
();
}
};
// TODO
1
as
*
mut
c_void
}
else
{
eprintln!
(
"dlopen: tcb not found"
);
ERROR
.store
(
ERROR_NOT_SUPPORTED
.as_ptr
()
as
usize
,
Ordering
::
SeqCst
);
ptr
::
null_mut
()
}
}
else
{
}
else
{
1
as
*
mut
c_void
1
as
*
mut
c_void
}
}
...
@@ -56,15 +89,38 @@ pub unsafe extern "C" fn dlopen(filename: *const c_char, flags: c_int) -> *mut c
...
@@ -56,15 +89,38 @@ pub unsafe extern "C" fn dlopen(filename: *const c_char, flags: c_int) -> *mut c
#[no_mangle]
#[no_mangle]
pub
unsafe
extern
"C"
fn
dlsym
(
handle
:
*
mut
c_void
,
symbol
:
*
const
c_char
)
->
*
mut
c_void
{
pub
unsafe
extern
"C"
fn
dlsym
(
handle
:
*
mut
c_void
,
symbol
:
*
const
c_char
)
->
*
mut
c_void
{
let
symbol_opt
=
if
symbol
.is_null
()
{
if
symbol
.is_null
()
{
None
ERROR
.store
(
ERROR_NOT_SUPPORTED
.as_ptr
()
as
usize
,
Ordering
::
SeqCst
);
}
else
{
return
ptr
::
null_mut
();
Some
(
str
::
from_utf8_unchecked
(
CStr
::
from_ptr
(
symbol
)
.to_bytes
()))
}
};
let
symbol_str
=
str
::
from_utf8_unchecked
(
CStr
::
from_ptr
(
symbol
)
.to_bytes
());
eprintln!
(
"dlsym({:p}, {})"
,
handle
,
symbol_str
);
if
let
Some
(
tcb
)
=
Tcb
::
current
()
{
if
tcb
.linker_ptr
.is_null
()
{
eprintln!
(
"dlopen: linker not found"
);
ERROR
.store
(
ERROR_NOT_SUPPORTED
.as_ptr
()
as
usize
,
Ordering
::
SeqCst
);
return
ptr
::
null_mut
();
}
eprintln!
(
"dlsym({:p}, {:?})"
,
handle
,
symbol_opt
);
eprintln!
(
"dlsym: linker_ptr: {:p}"
,
tcb
.linker_ptr
);
let
linker
=
(
&*
tcb
.linker_ptr
)
.lock
();
ptr
::
null_mut
()
if
let
Some
(
global
)
=
linker
.globals
.get
(
symbol_str
)
{
eprintln!
(
"dlsym({:p}, {}) = 0x{:x}"
,
handle
,
symbol_str
,
*
global
);
*
global
as
*
mut
c_void
}
else
{
eprintln!
(
"dlsym: symbol not found"
);
ERROR
.store
(
ERROR_NOT_SUPPORTED
.as_ptr
()
as
usize
,
Ordering
::
SeqCst
);
ptr
::
null_mut
()
}
}
else
{
eprintln!
(
"dlsym: tcb not found"
);
ERROR
.store
(
ERROR_NOT_SUPPORTED
.as_ptr
()
as
usize
,
Ordering
::
SeqCst
);
ptr
::
null_mut
()
}
}
}
#[no_mangle]
#[no_mangle]
...
...
This diff is collapsed.
Click to expand it.
src/ld_so/linker.rs
+
16
−
8
View file @
a3f7a174
...
@@ -38,7 +38,7 @@ pub struct Linker {
...
@@ -38,7 +38,7 @@ pub struct Linker {
// Used by link
// Used by link
/// Global symbols
/// Global symbols
globals
:
BTreeMap
<
String
,
usize
>
,
pub
globals
:
BTreeMap
<
String
,
usize
>
,
/// Loaded library in-memory data
/// Loaded library in-memory data
mmaps
:
BTreeMap
<
String
,
&
'static
mut
[
u8
]
>
,
mmaps
:
BTreeMap
<
String
,
&
'static
mut
[
u8
]
>
,
}
}
...
@@ -226,8 +226,12 @@ impl Linker {
...
@@ -226,8 +226,12 @@ impl Linker {
}
}
// Allocate TLS
// Allocate TLS
let
tcb
=
unsafe
{
Tcb
::
new
(
tls_size
)
?
};
let
mut
tcb_opt
=
if
primary_opt
.is_some
()
{
println!
(
"tcb {:x?}"
,
tcb
);
Some
(
unsafe
{
Tcb
::
new
(
tls_size
)
?
})
}
else
{
None
};
println!
(
"tcb {:x?}"
,
tcb_opt
);
// Copy data
// Copy data
let
mut
tls_offset
=
tls_primary
;
let
mut
tls_offset
=
tls_primary
;
...
@@ -333,9 +337,11 @@ impl Linker {
...
@@ -333,9 +337,11 @@ impl Linker {
}
}
// Set master images for TLS and copy TLS data
// Set master images for TLS and copy TLS data
unsafe
{
if
let
Some
(
ref
mut
tcb
)
=
tcb_opt
{
tcb
.set_masters
(
tcb_masters
.into_boxed_slice
());
unsafe
{
tcb
.copy_masters
()
?
;
tcb
.set_masters
(
tcb_masters
.into_boxed_slice
());
tcb
.copy_masters
()
?
;
}
}
}
// Perform relocations, and protect pages
// Perform relocations, and protect pages
...
@@ -468,8 +474,10 @@ impl Linker {
...
@@ -468,8 +474,10 @@ impl Linker {
}
}
// Activate TLS
// Activate TLS
unsafe
{
if
let
Some
(
ref
mut
tcb
)
=
tcb_opt
{
tcb
.activate
();
unsafe
{
tcb
.activate
();
}
}
}
// Perform indirect relocations (necessary evil), gather entry point
// Perform indirect relocations (necessary evil), gather entry point
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment