Skip to content

Instantly share code, notes, and snippets.

@cataphract
Last active March 27, 2023 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cataphract/d9286ea6250d9597f1daf54f8ba23c05 to your computer and use it in GitHub Desktop.
Save cataphract/d9286ea6250d9597f1daf54f8ba23c05 to your computer and use it in GitHub Desktop.
C++ with no standard lib
g++ -v -std=c++11 -fno-exceptions -fno-rtti -nostdlib \
/usr/lib/x86_64-linux-gnu/{Scrt1.o,crti.o} \
/usr/lib/gcc/x86_64-linux-gnu/11/crtbeginS.o \
example.cpp -o example \
-lgcc \
-Wl,--push-state -Wl,--as-needed -lgcc_s -Wl,--pop-state \
-lc -lgcc \
-Wl,--push-state -Wl,--as-needed -lgcc_s -Wl,--pop-state \
/usr/lib/gcc/x86_64-linux-gnu/11/crtendS.o \
/usr/lib/x86_64-linux-gnu/crtn.o
#include <cstdlib>
#include <cstdio>
#include <utility>
void* operator new(std::size_t size) {
return std::malloc(size);
}
void operator delete(void* ptr) noexcept {
std::free(ptr);
}
void* operator new[](std::size_t size) {
return std::malloc(size);
}
void operator delete[](void* ptr) noexcept {
std::free(ptr);
}
template <typename T>
class MyArray {
T *data;
std::size_t size;
public:
explicit MyArray(std::size_t size) : data(new T[size]), size(size) {}
~MyArray() { delete[] data; }
MyArray(const MyArray& other) = delete;
MyArray& operator=(const MyArray& other) = delete;
MyArray(MyArray&& other) noexcept : data(other.data), size(other.size) {
other.data = nullptr;
other.size = 0;
}
MyArray& operator=(MyArray&& other) noexcept {
if (this != &other) {
free(data);
data = other.data;
size = other.size;
other.data = nullptr;
other.size = 0;
}
return *this;
}
// Iterator implementation
T* begin() const { return data; }
T* end() const { return data + size; }
T& operator[](size_t index) {
return data[index];
}
};
int main() {
auto *array = new MyArray<int>{10};
for (std::size_t i = 0; i < 10; i++) {
(*array)[i] = i;
}
for (auto&& elem: *array) {
printf("%d\n", elem);
}
MyArray<int> array2{std::move(*array)};
for (auto&& elem: *array) {
printf("%d\n", elem);
}
for (auto&& elem: array2) {
printf("%d\n", elem);
}
return 0;
}
Symbol table '.dynsym' contains 9 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 0000000000000000 0 FUNC GLOBAL DEFAULT UND free@GLIBC_2.2.5 (2)
2: 0000000000000000 0 FUNC GLOBAL DEFAULT UND __libc_start_main@GLIBC_2.34 (3)
3: 0000000000000000 0 NOTYPE WEAK DEFAULT UND _ITM_deregisterTMCloneTable
4: 0000000000000000 0 FUNC GLOBAL DEFAULT UND printf@GLIBC_2.2.5 (2)
5: 0000000000000000 0 NOTYPE WEAK DEFAULT UND __gmon_start__
6: 0000000000000000 0 FUNC GLOBAL DEFAULT UND malloc@GLIBC_2.2.5 (2)
7: 0000000000000000 0 NOTYPE WEAK DEFAULT UND _ITM_registerTMCloneTable
8: 0000000000000000 0 FUNC WEAK DEFAULT UND __cxa_finalize@GLIBC_2.2.5 (2)
Symbol table '.symtab' contains 53 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 0000000000000000 0 FILE LOCAL DEFAULT ABS Scrt1.o
2: 000000000000038c 32 OBJECT LOCAL DEFAULT 4 __abi_tag
3: 0000000000000000 0 FILE LOCAL DEFAULT ABS crtstuff.c
4: 00000000000010d0 0 FUNC LOCAL DEFAULT 16 deregister_tm_clones
5: 0000000000001100 0 FUNC LOCAL DEFAULT 16 register_tm_clones
6: 0000000000001140 0 FUNC LOCAL DEFAULT 16 __do_global_dtors_aux
7: 0000000000004010 1 OBJECT LOCAL DEFAULT 26 completed.0
8: 0000000000003db0 0 OBJECT LOCAL DEFAULT 22 __do_global_dtors_aux_fini_array_entry
9: 0000000000001180 0 FUNC LOCAL DEFAULT 16 frame_dummy
10: 0000000000003da8 0 OBJECT LOCAL DEFAULT 21 __frame_dummy_init_array_entry
11: 0000000000000000 0 FILE LOCAL DEFAULT ABS example.cpp
12: 0000000000002004 1 OBJECT LOCAL DEFAULT 18 _ZStL19piecewise_construct
13: 0000000000000000 0 FILE LOCAL DEFAULT ABS crtstuff.c
14: 00000000000022a4 0 OBJECT LOCAL DEFAULT 20 __FRAME_END__
15: 0000000000000000 0 FILE LOCAL DEFAULT ABS
16: 0000000000003db8 0 OBJECT LOCAL DEFAULT 23 _DYNAMIC
17: 000000000000200c 0 NOTYPE LOCAL DEFAULT 19 __GNU_EH_FRAME_HDR
18: 0000000000003fa8 0 OBJECT LOCAL DEFAULT 24 _GLOBAL_OFFSET_TABLE_
19: 00000000000013b8 85 FUNC WEAK DEFAULT 16 _ZN7MyArrayIiEC2Em
20: 0000000000000000 0 FUNC GLOBAL DEFAULT UND free@GLIBC_2.2.5
21: 00000000000014c6 46 FUNC WEAK DEFAULT 16 _ZN7MyArrayIiED1Ev
22: 0000000000000000 0 FUNC GLOBAL DEFAULT UND __libc_start_main@GLIBC_2.34
23: 00000000000011a7 31 FUNC GLOBAL DEFAULT 16 _ZdlPv
24: 0000000000000000 0 NOTYPE WEAK DEFAULT UND _ITM_deregisterTMCloneTable
25: 0000000000004000 0 NOTYPE WEAK DEFAULT 25 data_start
26: 000000000000147e 72 FUNC WEAK DEFAULT 16 _ZN7MyArrayIiEC2EOS0_
27: 00000000000011c6 30 FUNC GLOBAL DEFAULT 16 _Znam
28: 0000000000004010 0 NOTYPE GLOBAL DEFAULT 25 _edata
29: 000000000000140e 36 FUNC WEAK DEFAULT 16 _ZN7MyArrayIiEixEm
30: 00000000000014f4 0 FUNC GLOBAL HIDDEN 17 _fini
31: 0000000000000000 0 FUNC GLOBAL DEFAULT UND printf@GLIBC_2.2.5
32: 0000000000004000 0 NOTYPE GLOBAL DEFAULT 25 __data_start
33: 0000000000001432 21 FUNC WEAK DEFAULT 16 _ZNK7MyArrayIiE5beginEv
34: 0000000000000000 0 NOTYPE WEAK DEFAULT UND __gmon_start__
35: 0000000000004008 0 OBJECT GLOBAL HIDDEN 25 __dso_handle
36: 000000000000147e 72 FUNC WEAK DEFAULT 16 _ZN7MyArrayIiEC1EOS0_
37: 0000000000002000 4 OBJECT GLOBAL DEFAULT 18 _IO_stdin_used
38: 0000000000000000 0 FUNC GLOBAL DEFAULT UND malloc@GLIBC_2.2.5
39: 00000000000011e4 31 FUNC GLOBAL DEFAULT 16 _ZdaPv
40: 0000000000004018 0 NOTYPE GLOBAL DEFAULT 26 _end
41: 00000000000010a0 38 FUNC GLOBAL DEFAULT 16 _start
42: 0000000000004010 0 NOTYPE GLOBAL DEFAULT 26 __bss_start
43: 0000000000001203 436 FUNC GLOBAL DEFAULT 16 main
44: 0000000000001448 36 FUNC WEAK DEFAULT 16 _ZNK7MyArrayIiE3endEv
45: 000000000000146c 18 FUNC WEAK DEFAULT 16 _ZSt4moveIR7MyArrayIiEEONSt16remove_referenceIT_E4typeEOS4_
46: 0000000000001189 30 FUNC GLOBAL DEFAULT 16 _Znwm
47: 00000000000014c6 46 FUNC WEAK DEFAULT 16 _ZN7MyArrayIiED2Ev
48: 0000000000004010 0 OBJECT GLOBAL HIDDEN 25 __TMC_END__
49: 0000000000000000 0 NOTYPE WEAK DEFAULT UND _ITM_registerTMCloneTable
50: 00000000000013b8 85 FUNC WEAK DEFAULT 16 _ZN7MyArrayIiEC1Em
51: 0000000000000000 0 FUNC WEAK DEFAULT UND __cxa_finalize@GLIBC_2.2.5
52: 0000000000001000 0 FUNC GLOBAL HIDDEN 12 _init
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment