authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-04-22 18:11:50-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-04-22 18:11:50-04:00
log21767144fc1a8627a109e81a164c55171c279d82
tree99ba3c7ec18227fe0abc01da0335851ef8535358
parentda2af9c613841552e9e47b6c9f0e9e4ee74894fb

linux: support VDSO for clock_gettime

also fix a compiler crash when using cmpxchg with nullable pointer

10 files changed, 783 insertions(+), 12 deletions(-)

CMakeLists.txt+1
......@@ -508,6 +508,7 @@ set(ZIG_STD_FILES
508508 "os/index.zig"
509509 "os/linux/errno.zig"
510510 "os/linux/index.zig"
511 "os/linux/vdso.zig"
511512 "os/linux/x86_64.zig"
512513 "os/path.zig"
513514 "os/time.zig"
src/codegen.cpp+10
......@@ -3561,6 +3561,16 @@ static LLVMValueRef ir_render_cmpxchg(CodeGen *g, IrExecutable *executable, IrIn
35613561 LLVMValueRef result_val = ZigLLVMBuildCmpXchg(g->builder, ptr_val, cmp_val, new_val,
35623562 success_order, failure_order, instruction->is_weak);
35633563
3564 TypeTableEntry *maybe_type = instruction->base.value.type;
3565 assert(maybe_type->id == TypeTableEntryIdMaybe);
3566 TypeTableEntry *child_type = maybe_type->data.maybe.child_type;
3567
3568 if (type_is_codegen_pointer(child_type)) {
3569 LLVMValueRef payload_val = LLVMBuildExtractValue(g->builder, result_val, 0, "");
3570 LLVMValueRef success_bit = LLVMBuildExtractValue(g->builder, result_val, 1, "");
3571 return LLVMBuildSelect(g->builder, success_bit, LLVMConstNull(child_type->type_ref), payload_val, "");
3572 }
3573
35643574 assert(instruction->tmp_ptr != nullptr);
35653575 assert(type_has_bits(instruction->type));
35663576
std/elf.zig+611
......@@ -7,6 +7,246 @@ const mem = std.mem;
77const debug = std.debug;
88const InStream = std.stream.InStream;
99
10pub const AT_NULL = 0;
11pub const AT_IGNORE = 1;
12pub const AT_EXECFD = 2;
13pub const AT_PHDR = 3;
14pub const AT_PHENT = 4;
15pub const AT_PHNUM = 5;
16pub const AT_PAGESZ = 6;
17pub const AT_BASE = 7;
18pub const AT_FLAGS = 8;
19pub const AT_ENTRY = 9;
20pub const AT_NOTELF = 10;
21pub const AT_UID = 11;
22pub const AT_EUID = 12;
23pub const AT_GID = 13;
24pub const AT_EGID = 14;
25pub const AT_CLKTCK = 17;
26pub const AT_PLATFORM = 15;
27pub const AT_HWCAP = 16;
28pub const AT_FPUCW = 18;
29pub const AT_DCACHEBSIZE = 19;
30pub const AT_ICACHEBSIZE = 20;
31pub const AT_UCACHEBSIZE = 21;
32pub const AT_IGNOREPPC = 22;
33pub const AT_SECURE = 23;
34pub const AT_BASE_PLATFORM = 24;
35pub const AT_RANDOM = 25;
36pub const AT_HWCAP2 = 26;
37pub const AT_EXECFN = 31;
38pub const AT_SYSINFO = 32;
39pub const AT_SYSINFO_EHDR = 33;
40pub const AT_L1I_CACHESHAPE = 34;
41pub const AT_L1D_CACHESHAPE = 35;
42pub const AT_L2_CACHESHAPE = 36;
43pub const AT_L3_CACHESHAPE = 37;
44pub const AT_L1I_CACHESIZE = 40;
45pub const AT_L1I_CACHEGEOMETRY = 41;
46pub const AT_L1D_CACHESIZE = 42;
47pub const AT_L1D_CACHEGEOMETRY = 43;
48pub const AT_L2_CACHESIZE = 44;
49pub const AT_L2_CACHEGEOMETRY = 45;
50pub const AT_L3_CACHESIZE = 46;
51pub const AT_L3_CACHEGEOMETRY = 47;
52
53pub const DT_NULL = 0;
54pub const DT_NEEDED = 1;
55pub const DT_PLTRELSZ = 2;
56pub const DT_PLTGOT = 3;
57pub const DT_HASH = 4;
58pub const DT_STRTAB = 5;
59pub const DT_SYMTAB = 6;
60pub const DT_RELA = 7;
61pub const DT_RELASZ = 8;
62pub const DT_RELAENT = 9;
63pub const DT_STRSZ = 10;
64pub const DT_SYMENT = 11;
65pub const DT_INIT = 12;
66pub const DT_FINI = 13;
67pub const DT_SONAME = 14;
68pub const DT_RPATH = 15;
69pub const DT_SYMBOLIC = 16;
70pub const DT_REL = 17;
71pub const DT_RELSZ = 18;
72pub const DT_RELENT = 19;
73pub const DT_PLTREL = 20;
74pub const DT_DEBUG = 21;
75pub const DT_TEXTREL = 22;
76pub const DT_JMPREL = 23;
77pub const DT_BIND_NOW = 24;
78pub const DT_INIT_ARRAY = 25;
79pub const DT_FINI_ARRAY = 26;
80pub const DT_INIT_ARRAYSZ = 27;
81pub const DT_FINI_ARRAYSZ = 28;
82pub const DT_RUNPATH = 29;
83pub const DT_FLAGS = 30;
84pub const DT_ENCODING = 32;
85pub const DT_PREINIT_ARRAY = 32;
86pub const DT_PREINIT_ARRAYSZ = 33;
87pub const DT_SYMTAB_SHNDX = 34;
88pub const DT_NUM = 35;
89pub const DT_LOOS = 0x6000000d;
90pub const DT_HIOS = 0x6ffff000;
91pub const DT_LOPROC = 0x70000000;
92pub const DT_HIPROC = 0x7fffffff;
93pub const DT_PROCNUM = DT_MIPS_NUM;
94
95pub const DT_VALRNGLO = 0x6ffffd00;
96pub const DT_GNU_PRELINKED = 0x6ffffdf5;
97pub const DT_GNU_CONFLICTSZ = 0x6ffffdf6;
98pub const DT_GNU_LIBLISTSZ = 0x6ffffdf7;
99pub const DT_CHECKSUM = 0x6ffffdf8;
100pub const DT_PLTPADSZ = 0x6ffffdf9;
101pub const DT_MOVEENT = 0x6ffffdfa;
102pub const DT_MOVESZ = 0x6ffffdfb;
103pub const DT_FEATURE_1 = 0x6ffffdfc;
104pub const DT_POSFLAG_1 = 0x6ffffdfd;
105
106pub const DT_SYMINSZ = 0x6ffffdfe;
107pub const DT_SYMINENT = 0x6ffffdff;
108pub const DT_VALRNGHI = 0x6ffffdff;
109pub const DT_VALNUM = 12;
110
111pub const DT_ADDRRNGLO = 0x6ffffe00;
112pub const DT_GNU_HASH = 0x6ffffef5;
113pub const DT_TLSDESC_PLT = 0x6ffffef6;
114pub const DT_TLSDESC_GOT = 0x6ffffef7;
115pub const DT_GNU_CONFLICT = 0x6ffffef8;
116pub const DT_GNU_LIBLIST = 0x6ffffef9;
117pub const DT_CONFIG = 0x6ffffefa;
118pub const DT_DEPAUDIT = 0x6ffffefb;
119pub const DT_AUDIT = 0x6ffffefc;
120pub const DT_PLTPAD = 0x6ffffefd;
121pub const DT_MOVETAB = 0x6ffffefe;
122pub const DT_SYMINFO = 0x6ffffeff;
123pub const DT_ADDRRNGHI = 0x6ffffeff;
124pub const DT_ADDRNUM = 11;
125
126
127pub const DT_VERSYM = 0x6ffffff0;
128
129pub const DT_RELACOUNT = 0x6ffffff9;
130pub const DT_RELCOUNT = 0x6ffffffa;
131
132
133pub const DT_FLAGS_1 = 0x6ffffffb;
134pub const DT_VERDEF = 0x6ffffffc;
135
136pub const DT_VERDEFNUM = 0x6ffffffd;
137pub const DT_VERNEED = 0x6ffffffe;
138
139pub const DT_VERNEEDNUM = 0x6fffffff;
140pub const DT_VERSIONTAGNUM = 16;
141
142
143
144pub const DT_AUXILIARY = 0x7ffffffd;
145pub const DT_FILTER = 0x7fffffff;
146pub const DT_EXTRANUM = 3;
147
148
149pub const DT_SPARC_REGISTER = 0x70000001;
150pub const DT_SPARC_NUM = 2;
151
152pub const DT_MIPS_RLD_VERSION = 0x70000001;
153pub const DT_MIPS_TIME_STAMP = 0x70000002;
154pub const DT_MIPS_ICHECKSUM = 0x70000003;
155pub const DT_MIPS_IVERSION = 0x70000004;
156pub const DT_MIPS_FLAGS = 0x70000005;
157pub const DT_MIPS_BASE_ADDRESS = 0x70000006;
158pub const DT_MIPS_MSYM = 0x70000007;
159pub const DT_MIPS_CONFLICT = 0x70000008;
160pub const DT_MIPS_LIBLIST = 0x70000009;
161pub const DT_MIPS_LOCAL_GOTNO = 0x7000000a;
162pub const DT_MIPS_CONFLICTNO = 0x7000000b;
163pub const DT_MIPS_LIBLISTNO = 0x70000010;
164pub const DT_MIPS_SYMTABNO = 0x70000011;
165pub const DT_MIPS_UNREFEXTNO = 0x70000012;
166pub const DT_MIPS_GOTSYM = 0x70000013;
167pub const DT_MIPS_HIPAGENO = 0x70000014;
168pub const DT_MIPS_RLD_MAP = 0x70000016;
169pub const DT_MIPS_DELTA_CLASS = 0x70000017;
170pub const DT_MIPS_DELTA_CLASS_NO = 0x70000018;
171
172pub const DT_MIPS_DELTA_INSTANCE = 0x70000019;
173pub const DT_MIPS_DELTA_INSTANCE_NO = 0x7000001a;
174
175pub const DT_MIPS_DELTA_RELOC = 0x7000001b;
176pub const DT_MIPS_DELTA_RELOC_NO = 0x7000001c;
177
178pub const DT_MIPS_DELTA_SYM = 0x7000001d;
179
180pub const DT_MIPS_DELTA_SYM_NO = 0x7000001e;
181
182pub const DT_MIPS_DELTA_CLASSSYM = 0x70000020;
183
184pub const DT_MIPS_DELTA_CLASSSYM_NO = 0x70000021;
185
186pub const DT_MIPS_CXX_FLAGS = 0x70000022;
187pub const DT_MIPS_PIXIE_INIT = 0x70000023;
188pub const DT_MIPS_SYMBOL_LIB = 0x70000024;
189pub const DT_MIPS_LOCALPAGE_GOTIDX = 0x70000025;
190pub const DT_MIPS_LOCAL_GOTIDX = 0x70000026;
191pub const DT_MIPS_HIDDEN_GOTIDX = 0x70000027;
192pub const DT_MIPS_PROTECTED_GOTIDX = 0x70000028;
193pub const DT_MIPS_OPTIONS = 0x70000029;
194pub const DT_MIPS_INTERFACE = 0x7000002a;
195pub const DT_MIPS_DYNSTR_ALIGN = 0x7000002b;
196pub const DT_MIPS_INTERFACE_SIZE = 0x7000002c;
197pub const DT_MIPS_RLD_TEXT_RESOLVE_ADDR = 0x7000002d;
198
199pub const DT_MIPS_PERF_SUFFIX = 0x7000002e;
200
201pub const DT_MIPS_COMPACT_SIZE = 0x7000002f;
202pub const DT_MIPS_GP_VALUE = 0x70000030;
203pub const DT_MIPS_AUX_DYNAMIC = 0x70000031;
204
205pub const DT_MIPS_PLTGOT = 0x70000032;
206
207pub const DT_MIPS_RWPLT = 0x70000034;
208pub const DT_MIPS_RLD_MAP_REL = 0x70000035;
209pub const DT_MIPS_NUM = 0x36;
210
211pub const DT_ALPHA_PLTRO = (DT_LOPROC + 0);
212pub const DT_ALPHA_NUM = 1;
213
214pub const DT_PPC_GOT = (DT_LOPROC + 0);
215pub const DT_PPC_OPT = (DT_LOPROC + 1);
216pub const DT_PPC_NUM = 2;
217
218pub const DT_PPC64_GLINK = (DT_LOPROC + 0);
219pub const DT_PPC64_OPD = (DT_LOPROC + 1);
220pub const DT_PPC64_OPDSZ = (DT_LOPROC + 2);
221pub const DT_PPC64_OPT = (DT_LOPROC + 3);
222pub const DT_PPC64_NUM = 4;
223
224pub const DT_IA_64_PLT_RESERVE = (DT_LOPROC + 0);
225pub const DT_IA_64_NUM = 1;
226
227pub const DT_NIOS2_GP = 0x70000002;
228
229pub const PT_NULL = 0;
230pub const PT_LOAD = 1;
231pub const PT_DYNAMIC = 2;
232pub const PT_INTERP = 3;
233pub const PT_NOTE = 4;
234pub const PT_SHLIB = 5;
235pub const PT_PHDR = 6;
236pub const PT_TLS = 7;
237pub const PT_NUM = 8;
238pub const PT_LOOS = 0x60000000;
239pub const PT_GNU_EH_FRAME = 0x6474e550;
240pub const PT_GNU_STACK = 0x6474e551;
241pub const PT_GNU_RELRO = 0x6474e552;
242pub const PT_LOSUNW = 0x6ffffffa;
243pub const PT_SUNWBSS = 0x6ffffffa;
244pub const PT_SUNWSTACK = 0x6ffffffb;
245pub const PT_HISUNW = 0x6fffffff;
246pub const PT_HIOS = 0x6fffffff;
247pub const PT_LOPROC = 0x70000000;
248pub const PT_HIPROC = 0x7fffffff;
249
10250pub const SHT_NULL = 0;
11251pub const SHT_PROGBITS = 1;
12252pub const SHT_SYMTAB = 2;
......@@ -31,6 +271,45 @@ pub const SHT_HIPROC = 0x7fffffff;
31271pub const SHT_LOUSER = 0x80000000;
32272pub const SHT_HIUSER = 0xffffffff;
33273
274pub const STB_LOCAL = 0;
275pub const STB_GLOBAL = 1;
276pub const STB_WEAK = 2;
277pub const STB_NUM = 3;
278pub const STB_LOOS = 10;
279pub const STB_GNU_UNIQUE = 10;
280pub const STB_HIOS = 12;
281pub const STB_LOPROC = 13;
282pub const STB_HIPROC = 15;
283
284pub const STB_MIPS_SPLIT_COMMON = 13;
285
286pub const STT_NOTYPE = 0;
287pub const STT_OBJECT = 1;
288pub const STT_FUNC = 2;
289pub const STT_SECTION = 3;
290pub const STT_FILE = 4;
291pub const STT_COMMON = 5;
292pub const STT_TLS = 6;
293pub const STT_NUM = 7;
294pub const STT_LOOS = 10;
295pub const STT_GNU_IFUNC = 10;
296pub const STT_HIOS = 12;
297pub const STT_LOPROC = 13;
298pub const STT_HIPROC = 15;
299
300pub const STT_SPARC_REGISTER = 13;
301
302pub const STT_PARISC_MILLICODE = 13;
303
304pub const STT_HP_OPAQUE = (STT_LOOS + 0x1);
305pub const STT_HP_STUB = (STT_LOOS + 0x2);
306
307pub const STT_ARM_TFUNC = STT_LOPROC;
308pub const STT_ARM_16BIT = STT_HIPROC;
309
310pub const VER_FLG_BASE = 0x1;
311pub const VER_FLG_WEAK = 0x2;
312
34313pub const FileType = enum {
35314 Relocatable,
36315 Executable,
......@@ -266,3 +545,335 @@ pub const Elf = struct {
266545 try elf.in_file.seekTo(elf_section.offset);
267546 }
268547};
548
549pub const EI_NIDENT = 16;
550pub const Elf32_Half = u16;
551pub const Elf64_Half = u16;
552pub const Elf32_Word = u32;
553pub const Elf32_Sword = i32;
554pub const Elf64_Word = u32;
555pub const Elf64_Sword = i32;
556pub const Elf32_Xword = u64;
557pub const Elf32_Sxword = i64;
558pub const Elf64_Xword = u64;
559pub const Elf64_Sxword = i64;
560pub const Elf32_Addr = u32;
561pub const Elf64_Addr = u64;
562pub const Elf32_Off = u32;
563pub const Elf64_Off = u64;
564pub const Elf32_Section = u16;
565pub const Elf64_Section = u16;
566pub const Elf32_Versym = Elf32_Half;
567pub const Elf64_Versym = Elf64_Half;
568pub const Elf32_Ehdr = extern struct {
569 e_ident: [EI_NIDENT]u8,
570 e_type: Elf32_Half,
571 e_machine: Elf32_Half,
572 e_version: Elf32_Word,
573 e_entry: Elf32_Addr,
574 e_phoff: Elf32_Off,
575 e_shoff: Elf32_Off,
576 e_flags: Elf32_Word,
577 e_ehsize: Elf32_Half,
578 e_phentsize: Elf32_Half,
579 e_phnum: Elf32_Half,
580 e_shentsize: Elf32_Half,
581 e_shnum: Elf32_Half,
582 e_shstrndx: Elf32_Half,
583};
584pub const Elf64_Ehdr = extern struct {
585 e_ident: [EI_NIDENT]u8,
586 e_type: Elf64_Half,
587 e_machine: Elf64_Half,
588 e_version: Elf64_Word,
589 e_entry: Elf64_Addr,
590 e_phoff: Elf64_Off,
591 e_shoff: Elf64_Off,
592 e_flags: Elf64_Word,
593 e_ehsize: Elf64_Half,
594 e_phentsize: Elf64_Half,
595 e_phnum: Elf64_Half,
596 e_shentsize: Elf64_Half,
597 e_shnum: Elf64_Half,
598 e_shstrndx: Elf64_Half,
599};
600pub const Elf32_Shdr = extern struct {
601 sh_name: Elf32_Word,
602 sh_type: Elf32_Word,
603 sh_flags: Elf32_Word,
604 sh_addr: Elf32_Addr,
605 sh_offset: Elf32_Off,
606 sh_size: Elf32_Word,
607 sh_link: Elf32_Word,
608 sh_info: Elf32_Word,
609 sh_addralign: Elf32_Word,
610 sh_entsize: Elf32_Word,
611};
612pub const Elf64_Shdr = extern struct {
613 sh_name: Elf64_Word,
614 sh_type: Elf64_Word,
615 sh_flags: Elf64_Xword,
616 sh_addr: Elf64_Addr,
617 sh_offset: Elf64_Off,
618 sh_size: Elf64_Xword,
619 sh_link: Elf64_Word,
620 sh_info: Elf64_Word,
621 sh_addralign: Elf64_Xword,
622 sh_entsize: Elf64_Xword,
623};
624pub const Elf32_Chdr = extern struct {
625 ch_type: Elf32_Word,
626 ch_size: Elf32_Word,
627 ch_addralign: Elf32_Word,
628};
629pub const Elf64_Chdr = extern struct {
630 ch_type: Elf64_Word,
631 ch_reserved: Elf64_Word,
632 ch_size: Elf64_Xword,
633 ch_addralign: Elf64_Xword,
634};
635pub const Elf32_Sym = extern struct {
636 st_name: Elf32_Word,
637 st_value: Elf32_Addr,
638 st_size: Elf32_Word,
639 st_info: u8,
640 st_other: u8,
641 st_shndx: Elf32_Section,
642};
643pub const Elf64_Sym = extern struct {
644 st_name: Elf64_Word,
645 st_info: u8,
646 st_other: u8,
647 st_shndx: Elf64_Section,
648 st_value: Elf64_Addr,
649 st_size: Elf64_Xword,
650};
651pub const Elf32_Syminfo = extern struct {
652 si_boundto: Elf32_Half,
653 si_flags: Elf32_Half,
654};
655pub const Elf64_Syminfo = extern struct {
656 si_boundto: Elf64_Half,
657 si_flags: Elf64_Half,
658};
659pub const Elf32_Rel = extern struct {
660 r_offset: Elf32_Addr,
661 r_info: Elf32_Word,
662};
663pub const Elf64_Rel = extern struct {
664 r_offset: Elf64_Addr,
665 r_info: Elf64_Xword,
666};
667pub const Elf32_Rela = extern struct {
668 r_offset: Elf32_Addr,
669 r_info: Elf32_Word,
670 r_addend: Elf32_Sword,
671};
672pub const Elf64_Rela = extern struct {
673 r_offset: Elf64_Addr,
674 r_info: Elf64_Xword,
675 r_addend: Elf64_Sxword,
676};
677pub const Elf32_Phdr = extern struct {
678 p_type: Elf32_Word,
679 p_offset: Elf32_Off,
680 p_vaddr: Elf32_Addr,
681 p_paddr: Elf32_Addr,
682 p_filesz: Elf32_Word,
683 p_memsz: Elf32_Word,
684 p_flags: Elf32_Word,
685 p_align: Elf32_Word,
686};
687pub const Elf64_Phdr = extern struct {
688 p_type: Elf64_Word,
689 p_flags: Elf64_Word,
690 p_offset: Elf64_Off,
691 p_vaddr: Elf64_Addr,
692 p_paddr: Elf64_Addr,
693 p_filesz: Elf64_Xword,
694 p_memsz: Elf64_Xword,
695 p_align: Elf64_Xword,
696};
697pub const Elf32_Dyn = extern struct {
698 d_tag: Elf32_Sword,
699 d_un: extern union {
700 d_val: Elf32_Word,
701 d_ptr: Elf32_Addr,
702 },
703};
704pub const Elf64_Dyn = extern struct {
705 d_tag: Elf64_Sxword,
706 d_un: extern union {
707 d_val: Elf64_Xword,
708 d_ptr: Elf64_Addr,
709 },
710};
711pub const Elf32_Verdef = extern struct {
712 vd_version: Elf32_Half,
713 vd_flags: Elf32_Half,
714 vd_ndx: Elf32_Half,
715 vd_cnt: Elf32_Half,
716 vd_hash: Elf32_Word,
717 vd_aux: Elf32_Word,
718 vd_next: Elf32_Word,
719};
720pub const Elf64_Verdef = extern struct {
721 vd_version: Elf64_Half,
722 vd_flags: Elf64_Half,
723 vd_ndx: Elf64_Half,
724 vd_cnt: Elf64_Half,
725 vd_hash: Elf64_Word,
726 vd_aux: Elf64_Word,
727 vd_next: Elf64_Word,
728};
729pub const Elf32_Verdaux = extern struct {
730 vda_name: Elf32_Word,
731 vda_next: Elf32_Word,
732};
733pub const Elf64_Verdaux = extern struct {
734 vda_name: Elf64_Word,
735 vda_next: Elf64_Word,
736};
737pub const Elf32_Verneed = extern struct {
738 vn_version: Elf32_Half,
739 vn_cnt: Elf32_Half,
740 vn_file: Elf32_Word,
741 vn_aux: Elf32_Word,
742 vn_next: Elf32_Word,
743};
744pub const Elf64_Verneed = extern struct {
745 vn_version: Elf64_Half,
746 vn_cnt: Elf64_Half,
747 vn_file: Elf64_Word,
748 vn_aux: Elf64_Word,
749 vn_next: Elf64_Word,
750};
751pub const Elf32_Vernaux = extern struct {
752 vna_hash: Elf32_Word,
753 vna_flags: Elf32_Half,
754 vna_other: Elf32_Half,
755 vna_name: Elf32_Word,
756 vna_next: Elf32_Word,
757};
758pub const Elf64_Vernaux = extern struct {
759 vna_hash: Elf64_Word,
760 vna_flags: Elf64_Half,
761 vna_other: Elf64_Half,
762 vna_name: Elf64_Word,
763 vna_next: Elf64_Word,
764};
765pub const Elf32_auxv_t = extern struct {
766 a_type: u32,
767 a_un: extern union {
768 a_val: u32,
769 },
770};
771pub const Elf64_auxv_t = extern struct {
772 a_type: u64,
773 a_un: extern union {
774 a_val: u64,
775 },
776};
777pub const Elf32_Nhdr = extern struct {
778 n_namesz: Elf32_Word,
779 n_descsz: Elf32_Word,
780 n_type: Elf32_Word,
781};
782pub const Elf64_Nhdr = extern struct {
783 n_namesz: Elf64_Word,
784 n_descsz: Elf64_Word,
785 n_type: Elf64_Word,
786};
787pub const Elf32_Move = extern struct {
788 m_value: Elf32_Xword,
789 m_info: Elf32_Word,
790 m_poffset: Elf32_Word,
791 m_repeat: Elf32_Half,
792 m_stride: Elf32_Half,
793};
794pub const Elf64_Move = extern struct {
795 m_value: Elf64_Xword,
796 m_info: Elf64_Xword,
797 m_poffset: Elf64_Xword,
798 m_repeat: Elf64_Half,
799 m_stride: Elf64_Half,
800};
801pub const Elf32_gptab = extern union {
802 gt_header: extern struct {
803 gt_current_g_value: Elf32_Word,
804 gt_unused: Elf32_Word,
805 },
806 gt_entry: extern struct {
807 gt_g_value: Elf32_Word,
808 gt_bytes: Elf32_Word,
809 },
810};
811pub const Elf32_RegInfo = extern struct {
812 ri_gprmask: Elf32_Word,
813 ri_cprmask: [4]Elf32_Word,
814 ri_gp_value: Elf32_Sword,
815};
816pub const Elf_Options = extern struct {
817 kind: u8,
818 size: u8,
819 @"section": Elf32_Section,
820 info: Elf32_Word,
821};
822pub const Elf_Options_Hw = extern struct {
823 hwp_flags1: Elf32_Word,
824 hwp_flags2: Elf32_Word,
825};
826pub const Elf32_Lib = extern struct {
827 l_name: Elf32_Word,
828 l_time_stamp: Elf32_Word,
829 l_checksum: Elf32_Word,
830 l_version: Elf32_Word,
831 l_flags: Elf32_Word,
832};
833pub const Elf64_Lib = extern struct {
834 l_name: Elf64_Word,
835 l_time_stamp: Elf64_Word,
836 l_checksum: Elf64_Word,
837 l_version: Elf64_Word,
838 l_flags: Elf64_Word,
839};
840pub const Elf32_Conflict = Elf32_Addr;
841pub const Elf_MIPS_ABIFlags_v0 = extern struct {
842 version: Elf32_Half,
843 isa_level: u8,
844 isa_rev: u8,
845 gpr_size: u8,
846 cpr1_size: u8,
847 cpr2_size: u8,
848 fp_abi: u8,
849 isa_ext: Elf32_Word,
850 ases: Elf32_Word,
851 flags1: Elf32_Word,
852 flags2: Elf32_Word,
853};
854
855pub const Ehdr = switch(@sizeOf(usize)) {
856 4 => Elf32_Ehdr,
857 8 => Elf64_Ehdr,
858 else => @compileError("expected pointer size of 32 or 64"),
859};
860pub const Phdr = switch(@sizeOf(usize)) {
861 4 => Elf32_Phdr,
862 8 => Elf64_Phdr,
863 else => @compileError("expected pointer size of 32 or 64"),
864};
865pub const Sym = switch(@sizeOf(usize)) {
866 4 => Elf32_Sym,
867 8 => Elf64_Sym,
868 else => @compileError("expected pointer size of 32 or 64"),
869};
870pub const Verdef = switch(@sizeOf(usize)) {
871 4 => Elf32_Verdef,
872 8 => Elf64_Verdef,
873 else => @compileError("expected pointer size of 32 or 64"),
874};
875pub const Verdaux = switch(@sizeOf(usize)) {
876 4 => Elf32_Verdaux,
877 8 => Elf64_Verdaux,
878 else => @compileError("expected pointer size of 32 or 64"),
879};
std/os/index.zig+1
......@@ -478,6 +478,7 @@ fn posixExecveErrnoToErr(err: usize) PosixExecveError {
478478 };
479479}
480480
481pub var linux_aux_raw = []usize{0} ** 38;
481482pub var posix_environ_raw: []&u8 = undefined;
482483
483484/// Caller must free result when done.
std/os/linux/index.zig+20
......@@ -1,6 +1,7 @@
11const std = @import("../../index.zig");
22const assert = std.debug.assert;
33const builtin = @import("builtin");
4const vdso = @import("vdso.zig");
45pub use switch (builtin.arch) {
56 builtin.Arch.x86_64 => @import("x86_64.zig"),
67 builtin.Arch.i386 => @import("i386.zig"),
......@@ -806,8 +807,27 @@ pub fn waitpid(pid: i32, status: &i32, options: i32) usize {
806807}
807808
808809pub fn clock_gettime(clk_id: i32, tp: &timespec) usize {
810 if (VDSO_CGT_SYM.len != 0) {
811 const f = @atomicLoad(@typeOf(init_vdso_clock_gettime), &vdso_clock_gettime, builtin.AtomicOrder.Unordered);
812 if (@ptrToInt(f) != 0) {
813 const rc = f(clk_id, tp);
814 switch (rc) {
815 0, @bitCast(usize, isize(-EINVAL)) => return rc,
816 else => {},
817 }
818 }
819 }
809820 return syscall2(SYS_clock_gettime, @bitCast(usize, isize(clk_id)), @ptrToInt(tp));
810821}
822var vdso_clock_gettime = init_vdso_clock_gettime;
823extern fn init_vdso_clock_gettime(clk: i32, ts: &timespec) usize {
824 const addr = vdso.lookup(VDSO_CGT_VER, VDSO_CGT_SYM);
825 var f = @intToPtr(@typeOf(init_vdso_clock_gettime), addr);
826 _ = @cmpxchgStrong(@typeOf(init_vdso_clock_gettime), &vdso_clock_gettime, init_vdso_clock_gettime, f,
827 builtin.AtomicOrder.Monotonic, builtin.AtomicOrder.Monotonic);
828 if (@ptrToInt(f) == 0) return @bitCast(usize, isize(-ENOSYS));
829 return f(clk, ts);
830}
811831
812832pub fn clock_getres(clk_id: i32, tp: &timespec) usize {
813833 return syscall2(SYS_clock_getres, @bitCast(usize, isize(clk_id)), @ptrToInt(tp));
std/os/linux/vdso.zig created+89
......@@ -0,0 +1,89 @@
1const std = @import("../../index.zig");
2const elf = std.elf;
3const linux = std.os.linux;
4const cstr = std.cstr;
5const mem = std.mem;
6
7pub fn lookup(vername: []const u8, name: []const u8) usize {
8 const vdso_addr = std.os.linux_aux_raw[std.elf.AT_SYSINFO_EHDR];
9 if (vdso_addr == 0) return 0;
10
11 const eh = @intToPtr(&elf.Ehdr, vdso_addr);
12 var ph_addr: usize = vdso_addr + eh.e_phoff;
13 const ph = @intToPtr(&elf.Phdr, ph_addr);
14
15 var maybe_dynv: ?&usize = null;
16 var base: usize = @maxValue(usize);
17 {
18 var i: usize = 0;
19 while (i < eh.e_phnum) : ({i += 1; ph_addr += eh.e_phentsize;}) {
20 const this_ph = @intToPtr(&elf.Phdr, ph_addr);
21 switch (this_ph.p_type) {
22 elf.PT_LOAD => base = vdso_addr + this_ph.p_offset - this_ph.p_vaddr,
23 elf.PT_DYNAMIC => maybe_dynv = @intToPtr(&usize, vdso_addr + this_ph.p_offset),
24 else => {},
25 }
26 }
27 }
28 const dynv = maybe_dynv ?? return 0;
29 if (base == @maxValue(usize)) return 0;
30
31 var maybe_strings: ?&u8 = null;
32 var maybe_syms: ?&elf.Sym = null;
33 var maybe_hashtab: ?&linux.Elf_Symndx = null;
34 var maybe_versym: ?&u16 = null;
35 var maybe_verdef: ?&elf.Verdef = null;
36
37 {
38 var i: usize = 0;
39 while (dynv[i] != 0) : (i += 2) {
40 const p = base + dynv[i + 1];
41 switch (dynv[i]) {
42 elf.DT_STRTAB => maybe_strings = @intToPtr(&u8, p),
43 elf.DT_SYMTAB => maybe_syms = @intToPtr(&elf.Sym, p),
44 elf.DT_HASH => maybe_hashtab = @intToPtr(&linux.Elf_Symndx, p),
45 elf.DT_VERSYM => maybe_versym = @intToPtr(&u16, p),
46 elf.DT_VERDEF => maybe_verdef = @intToPtr(&elf.Verdef, p),
47 else => {},
48 }
49 }
50 }
51
52 const strings = maybe_strings ?? return 0;
53 const syms = maybe_syms ?? return 0;
54 const hashtab = maybe_hashtab ?? return 0;
55 if (maybe_verdef == null) maybe_versym = null;
56
57
58 const OK_TYPES = (1<<elf.STT_NOTYPE | 1<<elf.STT_OBJECT | 1<<elf.STT_FUNC | 1<<elf.STT_COMMON);
59 const OK_BINDS = (1<<elf.STB_GLOBAL | 1<<elf.STB_WEAK | 1<<elf.STB_GNU_UNIQUE);
60
61 var i: usize = 0;
62 while (i < hashtab[1]) : (i += 1) {
63 if (0==(u32(1)<<u5(syms[i].st_info&0xf) & OK_TYPES)) continue;
64 if (0==(u32(1)<<u5(syms[i].st_info>>4) & OK_BINDS)) continue;
65 if (0==syms[i].st_shndx) continue;
66 if (!mem.eql(u8, name, cstr.toSliceConst(&strings[syms[i].st_name]))) continue;
67 if (maybe_versym) |versym| {
68 if (!checkver(??maybe_verdef, versym[i], vername, strings))
69 continue;
70 }
71 return base + syms[i].st_value;
72 }
73
74 return 0;
75}
76
77fn checkver(def_arg: &elf.Verdef, vsym_arg: i32, vername: []const u8, strings: &u8) bool {
78 var def = def_arg;
79 const vsym = @bitCast(u32, vsym_arg) & 0x7fff;
80 while (true) {
81 if (0==(def.vd_flags & elf.VER_FLG_BASE) and (def.vd_ndx & 0x7fff) == vsym)
82 break;
83 if (def.vd_next == 0)
84 return false;
85 def = @intToPtr(&elf.Verdef, @ptrToInt(def) + def.vd_next);
86 }
87 const aux = @intToPtr(&elf.Verdaux, @ptrToInt(def ) + def.vd_aux);
88 return mem.eql(u8, vername, cstr.toSliceConst(&strings[aux.vda_name]));
89}
std/os/linux/x86_64.zig+8
......@@ -371,6 +371,13 @@ pub const F_GETOWN_EX = 16;
371371
372372pub const F_GETOWNER_UIDS = 17;
373373
374
375pub const VDSO_USEFUL = true;
376pub const VDSO_CGT_SYM = "__vdso_clock_gettime";
377pub const VDSO_CGT_VER = "LINUX_2.6";
378pub const VDSO_GETCPU_SYM = "__vdso_getcpu";
379pub const VDSO_GETCPU_VER = "LINUX_2.6";
380
374381pub fn syscall0(number: usize) usize {
375382 return asm volatile ("syscall"
376383 : [ret] "={rax}" (-> usize)
......@@ -509,3 +516,4 @@ pub const dirent = extern struct {
509516 d_name: u8, // field address is the address of first byte of name
510517};
511518
519pub const Elf_Symndx = u32;
std/os/time.zig+4-4
......@@ -160,13 +160,13 @@ pub const Timer = struct {
160160 Os.windows => {
161161 var freq: i64 = undefined;
162162 var err = windows.QueryPerformanceFrequency(&freq);
163 if (err == 0) return error.TimerUnsupported;
163 if (err == windows.FALSE) return error.TimerUnsupported;
164164 self.frequency = u64(freq);
165165 self.resolution = @divFloor(ns_per_s, self.frequency);
166166
167167 var start_time: i64 = undefined;
168168 err = windows.QueryPerformanceCounter(&start_time);
169 debug.assert(err != 0);
169 debug.assert(err != windows.FALSE);
170170 self.start_time = u64(start_time);
171171 },
172172 Os.linux => {
......@@ -236,7 +236,7 @@ pub const Timer = struct {
236236 fn clockWindows() u64 {
237237 var result: i64 = undefined;
238238 var err = windows.QueryPerformanceCounter(&result);
239 debug.assert(err != 0);
239 debug.assert(err != windows.FALSE);
240240 return u64(result);
241241 }
242242
......@@ -285,4 +285,4 @@ test "os.time.Timer" {
285285
286286 timer.reset();
287287 debug.assert(timer.read() < time_1);
288}
\ No newline at end of file
288}
std/special/bootstrap.zig+19-8
......@@ -48,22 +48,33 @@ extern fn WinMainCRTStartup() noreturn {
4848fn posixCallMainAndExit() noreturn {
4949 const argc = *argc_ptr;
5050 const argv = @ptrCast(&&u8, &argc_ptr[1]);
51 const envp = @ptrCast(&?&u8, &argv[argc + 1]);
51 const envp_nullable = @ptrCast(&?&u8, &argv[argc + 1]);
52 var envp_count: usize = 0;
53 while (envp_nullable[envp_count]) |_| : (envp_count += 1) {}
54 const envp = @ptrCast(&&u8, envp_nullable)[0..envp_count];
55 if (builtin.os == builtin.Os.linux) {
56 const auxv = &@ptrCast(&usize, envp.ptr)[envp_count + 1];
57 var i: usize = 0;
58 while (auxv[i] != 0) : (i += 2) {
59 if (auxv[i] < std.os.linux_aux_raw.len) std.os.linux_aux_raw[auxv[i]] = auxv[i+1];
60 }
61 std.debug.assert(std.os.linux_aux_raw[std.elf.AT_PAGESZ] == std.os.page_size);
62 }
63
5264 std.os.posix.exit(callMainWithArgs(argc, argv, envp));
5365}
5466
55fn callMainWithArgs(argc: usize, argv: &&u8, envp: &?&u8) u8 {
67fn callMainWithArgs(argc: usize, argv: &&u8, envp: []&u8) u8 {
5668 std.os.ArgIteratorPosix.raw = argv[0..argc];
57
58 var env_count: usize = 0;
59 while (envp[env_count] != null) : (env_count += 1) {}
60 std.os.posix_environ_raw = @ptrCast(&&u8, envp)[0..env_count];
61
69 std.os.posix_environ_raw = envp;
6270 return callMain();
6371}
6472
6573extern fn main(c_argc: i32, c_argv: &&u8, c_envp: &?&u8) i32 {
66 return callMainWithArgs(usize(c_argc), c_argv, c_envp);
74 var env_count: usize = 0;
75 while (c_envp[env_count] != null) : (env_count += 1) {}
76 const envp = @ptrCast(&&u8, c_envp)[0..env_count];
77 return callMainWithArgs(usize(c_argc), c_argv, envp);
6778}
6879
6980fn callMain() u8 {
test/cases/atomics.zig+20
......@@ -49,3 +49,23 @@ fn testAtomicLoad(ptr: &u8) void {
4949 const x = @atomicLoad(u8, ptr, AtomicOrder.SeqCst);
5050 assert(x == 42);
5151}
52
53test "cmpxchg with ptr" {
54 var data1: i32 = 1234;
55 var data2: i32 = 5678;
56 var data3: i32 = 9101;
57 var x: &i32 = &data1;
58 if (@cmpxchgWeak(&i32, &x, &data2, &data3, AtomicOrder.SeqCst, AtomicOrder.SeqCst)) |x1| {
59 assert(x1 == &data1);
60 } else {
61 @panic("cmpxchg should have failed");
62 }
63
64 while (@cmpxchgWeak(&i32, &x, &data1, &data3, AtomicOrder.SeqCst, AtomicOrder.SeqCst)) |x1| {
65 assert(x1 == &data1);
66 }
67 assert(x == &data3);
68
69 assert(@cmpxchgStrong(&i32, &x, &data3, &data2, AtomicOrder.SeqCst, AtomicOrder.SeqCst) == null);
70 assert(x == &data2);
71}