authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-08-25 03:07:37-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-08-25 03:07:37-04:00
logac36f98e72f7ef33346bc772e4f257b79d04fcff
tree88fc4a2e89dcecbfbb4c42eee9ed6411512d45c9
parent32901926f088cd9617e7d98e23b0b056b5495193

fix stack traces on linux


12 files changed, 727 insertions(+), 592 deletions(-)

CMakeLists.txt+1
......@@ -485,6 +485,7 @@ set(ZIG_STD_FILES
485485 "json.zig"
486486 "lazy_init.zig"
487487 "linked_list.zig"
488 "macho.zig"
488489 "math/acos.zig"
489490 "math/acosh.zig"
490491 "math/asin.zig"
src/analyze.cpp+115-85
......@@ -19,12 +19,12 @@
1919
2020static const size_t default_backward_branch_quota = 1000;
2121
22static void resolve_enum_type(CodeGen *g, TypeTableEntry *enum_type);
23static void resolve_struct_type(CodeGen *g, TypeTableEntry *struct_type);
22static Error resolve_enum_type(CodeGen *g, TypeTableEntry *enum_type);
23static Error resolve_struct_type(CodeGen *g, TypeTableEntry *struct_type);
2424
25static void resolve_struct_zero_bits(CodeGen *g, TypeTableEntry *struct_type);
26static void resolve_enum_zero_bits(CodeGen *g, TypeTableEntry *enum_type);
27static void resolve_union_zero_bits(CodeGen *g, TypeTableEntry *union_type);
25static Error ATTRIBUTE_MUST_USE resolve_struct_zero_bits(CodeGen *g, TypeTableEntry *struct_type);
26static Error ATTRIBUTE_MUST_USE resolve_enum_zero_bits(CodeGen *g, TypeTableEntry *enum_type);
27static Error ATTRIBUTE_MUST_USE resolve_union_zero_bits(CodeGen *g, TypeTableEntry *union_type);
2828static void analyze_fn_body(CodeGen *g, FnTableEntry *fn_table_entry);
2929
3030ErrorMsg *add_node_error(CodeGen *g, AstNode *node, Buf *msg) {
......@@ -370,15 +370,20 @@ uint64_t type_size_bits(CodeGen *g, TypeTableEntry *type_entry) {
370370 return LLVMSizeOfTypeInBits(g->target_data_ref, type_entry->type_ref);
371371}
372372
373bool type_is_copyable(CodeGen *g, TypeTableEntry *type_entry) {
374 type_ensure_zero_bits_known(g, type_entry);
373Result<bool> type_is_copyable(CodeGen *g, TypeTableEntry *type_entry) {
374 Error err;
375 if ((err = type_ensure_zero_bits_known(g, type_entry)))
376 return err;
377
375378 if (!type_has_bits(type_entry))
376379 return true;
377380
378381 if (!handle_is_ptr(type_entry))
379382 return true;
380383
381 ensure_complete_type(g, type_entry);
384 if ((err = ensure_complete_type(g, type_entry)))
385 return err;
386
382387 return type_entry->is_copyable;
383388}
384389
......@@ -447,7 +452,7 @@ TypeTableEntry *get_pointer_to_type_extra(CodeGen *g, TypeTableEntry *child_type
447452 }
448453 }
449454
450 type_ensure_zero_bits_known(g, child_type);
455 assertNoError(type_ensure_zero_bits_known(g, child_type));
451456
452457 TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdPointer);
453458 entry->is_copyable = true;
......@@ -554,11 +559,11 @@ TypeTableEntry *get_optional_type(CodeGen *g, TypeTableEntry *child_type) {
554559 TypeTableEntry *entry = child_type->optional_parent;
555560 return entry;
556561 } else {
557 ensure_complete_type(g, child_type);
562 assertNoError(ensure_complete_type(g, child_type));
558563
559564 TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdOptional);
560565 assert(child_type->type_ref || child_type->zero_bits);
561 entry->is_copyable = type_is_copyable(g, child_type);
566 entry->is_copyable = type_is_copyable(g, child_type).unwrap();
562567
563568 buf_resize(&entry->name, 0);
564569 buf_appendf(&entry->name, "?%s", buf_ptr(&child_type->name));
......@@ -650,7 +655,7 @@ TypeTableEntry *get_error_union_type(CodeGen *g, TypeTableEntry *err_set_type, T
650655 TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdErrorUnion);
651656 entry->is_copyable = true;
652657 assert(payload_type->di_type);
653 ensure_complete_type(g, payload_type);
658 assertNoError(ensure_complete_type(g, payload_type));
654659
655660 buf_resize(&entry->name, 0);
656661 buf_appendf(&entry->name, "%s!%s", buf_ptr(&err_set_type->name), buf_ptr(&payload_type->name));
......@@ -739,7 +744,7 @@ TypeTableEntry *get_array_type(CodeGen *g, TypeTableEntry *child_type, uint64_t
739744 return entry;
740745 }
741746
742 ensure_complete_type(g, child_type);
747 assertNoError(ensure_complete_type(g, child_type));
743748
744749 TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdArray);
745750 entry->zero_bits = (array_size == 0) || child_type->zero_bits;
......@@ -1050,13 +1055,13 @@ TypeTableEntry *get_ptr_to_stack_trace_type(CodeGen *g) {
10501055}
10511056
10521057TypeTableEntry *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
1058 Error err;
10531059 auto table_entry = g->fn_type_table.maybe_get(fn_type_id);
10541060 if (table_entry) {
10551061 return table_entry->value;
10561062 }
10571063 if (fn_type_id->return_type != nullptr) {
1058 ensure_complete_type(g, fn_type_id->return_type);
1059 if (type_is_invalid(fn_type_id->return_type))
1064 if ((err = ensure_complete_type(g, fn_type_id->return_type)))
10601065 return g->builtin_types.entry_invalid;
10611066 assert(fn_type_id->return_type->id != TypeTableEntryIdOpaque);
10621067 } else {
......@@ -1172,8 +1177,7 @@ TypeTableEntry *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
11721177 gen_param_info->src_index = i;
11731178 gen_param_info->gen_index = SIZE_MAX;
11741179
1175 ensure_complete_type(g, type_entry);
1176 if (type_is_invalid(type_entry))
1180 if ((err = ensure_complete_type(g, type_entry)))
11771181 return g->builtin_types.entry_invalid;
11781182
11791183 if (type_has_bits(type_entry)) {
......@@ -1493,6 +1497,7 @@ TypeTableEntry *get_auto_err_set_type(CodeGen *g, FnTableEntry *fn_entry) {
14931497static TypeTableEntry *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *child_scope, FnTableEntry *fn_entry) {
14941498 assert(proto_node->type == NodeTypeFnProto);
14951499 AstNodeFnProto *fn_proto = &proto_node->data.fn_proto;
1500 Error err;
14961501
14971502 FnTypeId fn_type_id = {0};
14981503 init_fn_type_id(&fn_type_id, proto_node, proto_node->data.fn_proto.params.length);
......@@ -1550,7 +1555,8 @@ static TypeTableEntry *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *c
15501555 return g->builtin_types.entry_invalid;
15511556 }
15521557 if (!calling_convention_allows_zig_types(fn_type_id.cc)) {
1553 type_ensure_zero_bits_known(g, type_entry);
1558 if ((err = type_ensure_zero_bits_known(g, type_entry)))
1559 return g->builtin_types.entry_invalid;
15541560 if (!type_has_bits(type_entry)) {
15551561 add_node_error(g, param_node->data.param_decl.type,
15561562 buf_sprintf("parameter of type '%s' has 0 bits; not allowed in function with calling convention '%s'",
......@@ -1598,7 +1604,8 @@ static TypeTableEntry *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *c
15981604 case TypeTableEntryIdUnion:
15991605 case TypeTableEntryIdFn:
16001606 case TypeTableEntryIdPromise:
1601 type_ensure_zero_bits_known(g, type_entry);
1607 if ((err = type_ensure_zero_bits_known(g, type_entry)))
1608 return g->builtin_types.entry_invalid;
16021609 if (type_requires_comptime(type_entry)) {
16031610 add_node_error(g, param_node->data.param_decl.type,
16041611 buf_sprintf("parameter of type '%s' must be declared comptime",
......@@ -1729,24 +1736,25 @@ bool type_is_invalid(TypeTableEntry *type_entry) {
17291736}
17301737
17311738
1732static void resolve_enum_type(CodeGen *g, TypeTableEntry *enum_type) {
1739static Error resolve_enum_type(CodeGen *g, TypeTableEntry *enum_type) {
17331740 assert(enum_type->id == TypeTableEntryIdEnum);
17341741
17351742 if (enum_type->data.enumeration.complete)
1736 return;
1743 return ErrorNone;
17371744
1738 resolve_enum_zero_bits(g, enum_type);
1739 if (type_is_invalid(enum_type))
1740 return;
1745 Error err;
1746 if ((err = resolve_enum_zero_bits(g, enum_type)))
1747 return err;
17411748
17421749 AstNode *decl_node = enum_type->data.enumeration.decl_node;
17431750
17441751 if (enum_type->data.enumeration.embedded_in_current) {
17451752 if (!enum_type->data.enumeration.reported_infinite_err) {
1753 enum_type->data.enumeration.is_invalid = true;
17461754 enum_type->data.enumeration.reported_infinite_err = true;
17471755 add_node_error(g, decl_node, buf_sprintf("enum '%s' contains itself", buf_ptr(&enum_type->name)));
17481756 }
1749 return;
1757 return ErrorSemanticAnalyzeFail;
17501758 }
17511759
17521760 assert(!enum_type->data.enumeration.zero_bits_loop_flag);
......@@ -1778,7 +1786,7 @@ static void resolve_enum_type(CodeGen *g, TypeTableEntry *enum_type) {
17781786 enum_type->data.enumeration.complete = true;
17791787
17801788 if (enum_type->data.enumeration.is_invalid)
1781 return;
1789 return ErrorSemanticAnalyzeFail;
17821790
17831791 if (enum_type->zero_bits) {
17841792 enum_type->type_ref = LLVMVoidType();
......@@ -1797,7 +1805,7 @@ static void resolve_enum_type(CodeGen *g, TypeTableEntry *enum_type) {
17971805
17981806 ZigLLVMReplaceTemporary(g->dbuilder, enum_type->di_type, replacement_di_type);
17991807 enum_type->di_type = replacement_di_type;
1800 return;
1808 return ErrorNone;
18011809 }
18021810
18031811 TypeTableEntry *tag_int_type = enum_type->data.enumeration.tag_int_type;
......@@ -1815,6 +1823,7 @@ static void resolve_enum_type(CodeGen *g, TypeTableEntry *enum_type) {
18151823
18161824 ZigLLVMReplaceTemporary(g->dbuilder, enum_type->di_type, tag_di_type);
18171825 enum_type->di_type = tag_di_type;
1826 return ErrorNone;
18181827}
18191828
18201829
......@@ -1897,15 +1906,15 @@ TypeTableEntry *get_struct_type(CodeGen *g, const char *type_name, const char *f
18971906 return struct_type;
18981907}
18991908
1900static void resolve_struct_type(CodeGen *g, TypeTableEntry *struct_type) {
1909static Error resolve_struct_type(CodeGen *g, TypeTableEntry *struct_type) {
19011910 assert(struct_type->id == TypeTableEntryIdStruct);
19021911
19031912 if (struct_type->data.structure.complete)
1904 return;
1913 return ErrorNone;
19051914
1906 resolve_struct_zero_bits(g, struct_type);
1907 if (struct_type->data.structure.is_invalid)
1908 return;
1915 Error err;
1916 if ((err = resolve_struct_zero_bits(g, struct_type)))
1917 return err;
19091918
19101919 AstNode *decl_node = struct_type->data.structure.decl_node;
19111920
......@@ -1916,7 +1925,7 @@ static void resolve_struct_type(CodeGen *g, TypeTableEntry *struct_type) {
19161925 add_node_error(g, decl_node,
19171926 buf_sprintf("struct '%s' contains itself", buf_ptr(&struct_type->name)));
19181927 }
1919 return;
1928 return ErrorSemanticAnalyzeFail;
19201929 }
19211930
19221931 assert(!struct_type->data.structure.zero_bits_loop_flag);
......@@ -1943,8 +1952,7 @@ static void resolve_struct_type(CodeGen *g, TypeTableEntry *struct_type) {
19431952 TypeStructField *type_struct_field = &struct_type->data.structure.fields[i];
19441953 TypeTableEntry *field_type = type_struct_field->type_entry;
19451954
1946 ensure_complete_type(g, field_type);
1947 if (type_is_invalid(field_type)) {
1955 if ((err = ensure_complete_type(g, field_type))) {
19481956 struct_type->data.structure.is_invalid = true;
19491957 break;
19501958 }
......@@ -2026,7 +2034,7 @@ static void resolve_struct_type(CodeGen *g, TypeTableEntry *struct_type) {
20262034 struct_type->data.structure.complete = true;
20272035
20282036 if (struct_type->data.structure.is_invalid)
2029 return;
2037 return ErrorSemanticAnalyzeFail;
20302038
20312039 if (struct_type->zero_bits) {
20322040 struct_type->type_ref = LLVMVoidType();
......@@ -2045,7 +2053,7 @@ static void resolve_struct_type(CodeGen *g, TypeTableEntry *struct_type) {
20452053 0, nullptr, di_element_types, (int)debug_field_count, 0, nullptr, "");
20462054 ZigLLVMReplaceTemporary(g->dbuilder, struct_type->di_type, replacement_di_type);
20472055 struct_type->di_type = replacement_di_type;
2048 return;
2056 return ErrorNone;
20492057 }
20502058 assert(struct_type->di_type);
20512059
......@@ -2128,17 +2136,19 @@ static void resolve_struct_type(CodeGen *g, TypeTableEntry *struct_type) {
21282136
21292137 ZigLLVMReplaceTemporary(g->dbuilder, struct_type->di_type, replacement_di_type);
21302138 struct_type->di_type = replacement_di_type;
2139
2140 return ErrorNone;
21312141}
21322142
2133static void resolve_union_type(CodeGen *g, TypeTableEntry *union_type) {
2143static Error resolve_union_type(CodeGen *g, TypeTableEntry *union_type) {
21342144 assert(union_type->id == TypeTableEntryIdUnion);
21352145
21362146 if (union_type->data.unionation.complete)
2137 return;
2147 return ErrorNone;
21382148
2139 resolve_union_zero_bits(g, union_type);
2140 if (type_is_invalid(union_type))
2141 return;
2149 Error err;
2150 if ((err = resolve_union_zero_bits(g, union_type)))
2151 return err;
21422152
21432153 AstNode *decl_node = union_type->data.unionation.decl_node;
21442154
......@@ -2148,7 +2158,7 @@ static void resolve_union_type(CodeGen *g, TypeTableEntry *union_type) {
21482158 union_type->data.unionation.is_invalid = true;
21492159 add_node_error(g, decl_node, buf_sprintf("union '%s' contains itself", buf_ptr(&union_type->name)));
21502160 }
2151 return;
2161 return ErrorSemanticAnalyzeFail;
21522162 }
21532163
21542164 assert(!union_type->data.unionation.zero_bits_loop_flag);
......@@ -2179,8 +2189,7 @@ static void resolve_union_type(CodeGen *g, TypeTableEntry *union_type) {
21792189 TypeUnionField *union_field = &union_type->data.unionation.fields[i];
21802190 TypeTableEntry *field_type = union_field->type_entry;
21812191
2182 ensure_complete_type(g, field_type);
2183 if (type_is_invalid(field_type)) {
2192 if ((err = ensure_complete_type(g, field_type))) {
21842193 union_type->data.unionation.is_invalid = true;
21852194 continue;
21862195 }
......@@ -2219,7 +2228,7 @@ static void resolve_union_type(CodeGen *g, TypeTableEntry *union_type) {
22192228 union_type->data.unionation.most_aligned_union_member = most_aligned_union_member;
22202229
22212230 if (union_type->data.unionation.is_invalid)
2222 return;
2231 return ErrorSemanticAnalyzeFail;
22232232
22242233 if (union_type->zero_bits) {
22252234 union_type->type_ref = LLVMVoidType();
......@@ -2238,7 +2247,7 @@ static void resolve_union_type(CodeGen *g, TypeTableEntry *union_type) {
22382247
22392248 ZigLLVMReplaceTemporary(g->dbuilder, union_type->di_type, replacement_di_type);
22402249 union_type->di_type = replacement_di_type;
2241 return;
2250 return ErrorNone;
22422251 }
22432252
22442253 uint64_t padding_in_bits = biggest_size_in_bits - size_of_most_aligned_member_in_bits;
......@@ -2274,7 +2283,7 @@ static void resolve_union_type(CodeGen *g, TypeTableEntry *union_type) {
22742283
22752284 ZigLLVMReplaceTemporary(g->dbuilder, union_type->di_type, replacement_di_type);
22762285 union_type->di_type = replacement_di_type;
2277 return;
2286 return ErrorNone;
22782287 }
22792288
22802289 LLVMTypeRef union_type_ref;
......@@ -2293,7 +2302,7 @@ static void resolve_union_type(CodeGen *g, TypeTableEntry *union_type) {
22932302
22942303 ZigLLVMReplaceTemporary(g->dbuilder, union_type->di_type, tag_type->di_type);
22952304 union_type->di_type = tag_type->di_type;
2296 return;
2305 return ErrorNone;
22972306 } else {
22982307 union_type_ref = most_aligned_union_member->type_ref;
22992308 }
......@@ -2367,19 +2376,21 @@ static void resolve_union_type(CodeGen *g, TypeTableEntry *union_type) {
23672376
23682377 ZigLLVMReplaceTemporary(g->dbuilder, union_type->di_type, replacement_di_type);
23692378 union_type->di_type = replacement_di_type;
2379
2380 return ErrorNone;
23702381}
23712382
2372static void resolve_enum_zero_bits(CodeGen *g, TypeTableEntry *enum_type) {
2383static Error resolve_enum_zero_bits(CodeGen *g, TypeTableEntry *enum_type) {
23732384 assert(enum_type->id == TypeTableEntryIdEnum);
23742385
23752386 if (enum_type->data.enumeration.zero_bits_known)
2376 return;
2387 return ErrorNone;
23772388
23782389 if (enum_type->data.enumeration.zero_bits_loop_flag) {
23792390 add_node_error(g, enum_type->data.enumeration.decl_node,
23802391 buf_sprintf("'%s' depends on itself", buf_ptr(&enum_type->name)));
23812392 enum_type->data.enumeration.is_invalid = true;
2382 return;
2393 return ErrorSemanticAnalyzeFail;
23832394 }
23842395
23852396 enum_type->data.enumeration.zero_bits_loop_flag = true;
......@@ -2398,7 +2409,7 @@ static void resolve_enum_zero_bits(CodeGen *g, TypeTableEntry *enum_type) {
23982409 enum_type->data.enumeration.is_invalid = true;
23992410 enum_type->data.enumeration.zero_bits_loop_flag = false;
24002411 enum_type->data.enumeration.zero_bits_known = true;
2401 return;
2412 return ErrorSemanticAnalyzeFail;
24022413 }
24032414
24042415 enum_type->data.enumeration.src_field_count = field_count;
......@@ -2525,13 +2536,18 @@ static void resolve_enum_zero_bits(CodeGen *g, TypeTableEntry *enum_type) {
25252536 enum_type->data.enumeration.zero_bits_loop_flag = false;
25262537 enum_type->zero_bits = !type_has_bits(tag_int_type);
25272538 enum_type->data.enumeration.zero_bits_known = true;
2539 assert(!enum_type->data.enumeration.is_invalid);
2540
2541 return ErrorNone;
25282542}
25292543
2530static void resolve_struct_zero_bits(CodeGen *g, TypeTableEntry *struct_type) {
2544static Error resolve_struct_zero_bits(CodeGen *g, TypeTableEntry *struct_type) {
25312545 assert(struct_type->id == TypeTableEntryIdStruct);
25322546
2547 Error err;
2548
25332549 if (struct_type->data.structure.zero_bits_known)
2534 return;
2550 return ErrorNone;
25352551
25362552 if (struct_type->data.structure.zero_bits_loop_flag) {
25372553 // If we get here it's due to recursion. This is a design flaw in the compiler,
......@@ -2547,7 +2563,7 @@ static void resolve_struct_zero_bits(CodeGen *g, TypeTableEntry *struct_type) {
25472563 struct_type->data.structure.abi_alignment = LLVMABIAlignmentOfType(g->target_data_ref, LLVMPointerType(LLVMInt8Type(), 0));
25482564 }
25492565 }
2550 return;
2566 return ErrorNone;
25512567 }
25522568
25532569 struct_type->data.structure.zero_bits_loop_flag = true;
......@@ -2596,8 +2612,7 @@ static void resolve_struct_zero_bits(CodeGen *g, TypeTableEntry *struct_type) {
25962612 buf_sprintf("enums, not structs, support field assignment"));
25972613 }
25982614
2599 type_ensure_zero_bits_known(g, field_type);
2600 if (type_is_invalid(field_type)) {
2615 if ((err = type_ensure_zero_bits_known(g, field_type))) {
26012616 struct_type->data.structure.is_invalid = true;
26022617 continue;
26032618 }
......@@ -2634,16 +2649,24 @@ static void resolve_struct_zero_bits(CodeGen *g, TypeTableEntry *struct_type) {
26342649 struct_type->data.structure.gen_field_count = (uint32_t)gen_field_index;
26352650 struct_type->zero_bits = (gen_field_index == 0);
26362651 struct_type->data.structure.zero_bits_known = true;
2652
2653 if (struct_type->data.structure.is_invalid) {
2654 return ErrorSemanticAnalyzeFail;
2655 }
2656
2657 return ErrorNone;
26372658}
26382659
2639static void resolve_union_zero_bits(CodeGen *g, TypeTableEntry *union_type) {
2660static Error resolve_union_zero_bits(CodeGen *g, TypeTableEntry *union_type) {
26402661 assert(union_type->id == TypeTableEntryIdUnion);
26412662
2663 Error err;
2664
26422665 if (union_type->data.unionation.zero_bits_known)
2643 return;
2666 return ErrorNone;
26442667
26452668 if (type_is_invalid(union_type))
2646 return;
2669 return ErrorSemanticAnalyzeFail;
26472670
26482671 if (union_type->data.unionation.zero_bits_loop_flag) {
26492672 // If we get here it's due to recursion. From this we conclude that the struct is
......@@ -2660,7 +2683,7 @@ static void resolve_union_zero_bits(CodeGen *g, TypeTableEntry *union_type) {
26602683 LLVMPointerType(LLVMInt8Type(), 0));
26612684 }
26622685 }
2663 return;
2686 return ErrorNone;
26642687 }
26652688
26662689 union_type->data.unionation.zero_bits_loop_flag = true;
......@@ -2679,7 +2702,7 @@ static void resolve_union_zero_bits(CodeGen *g, TypeTableEntry *union_type) {
26792702 union_type->data.unionation.is_invalid = true;
26802703 union_type->data.unionation.zero_bits_loop_flag = false;
26812704 union_type->data.unionation.zero_bits_known = true;
2682 return;
2705 return ErrorSemanticAnalyzeFail;
26832706 }
26842707 union_type->data.unionation.src_field_count = field_count;
26852708 union_type->data.unionation.fields = allocate<TypeUnionField>(field_count);
......@@ -2711,13 +2734,13 @@ static void resolve_union_zero_bits(CodeGen *g, TypeTableEntry *union_type) {
27112734 tag_int_type = analyze_type_expr(g, scope, enum_type_node);
27122735 if (type_is_invalid(tag_int_type)) {
27132736 union_type->data.unionation.is_invalid = true;
2714 return;
2737 return ErrorSemanticAnalyzeFail;
27152738 }
27162739 if (tag_int_type->id != TypeTableEntryIdInt) {
27172740 add_node_error(g, enum_type_node,
27182741 buf_sprintf("expected integer tag type, found '%s'", buf_ptr(&tag_int_type->name)));
27192742 union_type->data.unionation.is_invalid = true;
2720 return;
2743 return ErrorSemanticAnalyzeFail;
27212744 }
27222745 } else {
27232746 tag_int_type = get_smallest_unsigned_int_type(g, field_count - 1);
......@@ -2744,13 +2767,13 @@ static void resolve_union_zero_bits(CodeGen *g, TypeTableEntry *union_type) {
27442767 TypeTableEntry *enum_type = analyze_type_expr(g, scope, enum_type_node);
27452768 if (type_is_invalid(enum_type)) {
27462769 union_type->data.unionation.is_invalid = true;
2747 return;
2770 return ErrorSemanticAnalyzeFail;
27482771 }
27492772 if (enum_type->id != TypeTableEntryIdEnum) {
27502773 union_type->data.unionation.is_invalid = true;
27512774 add_node_error(g, enum_type_node,
27522775 buf_sprintf("expected enum tag type, found '%s'", buf_ptr(&enum_type->name)));
2753 return;
2776 return ErrorSemanticAnalyzeFail;
27542777 }
27552778 tag_type = enum_type;
27562779 abi_alignment_so_far = get_abi_alignment(g, enum_type); // this populates src_field_count
......@@ -2789,8 +2812,7 @@ static void resolve_union_zero_bits(CodeGen *g, TypeTableEntry *union_type) {
27892812 }
27902813 } else {
27912814 field_type = analyze_type_expr(g, scope, field_node->data.struct_field.type);
2792 type_ensure_zero_bits_known(g, field_type);
2793 if (type_is_invalid(field_type)) {
2815 if ((err = type_ensure_zero_bits_known(g, field_type))) {
27942816 union_type->data.unionation.is_invalid = true;
27952817 continue;
27962818 }
......@@ -2883,7 +2905,7 @@ static void resolve_union_zero_bits(CodeGen *g, TypeTableEntry *union_type) {
28832905 union_type->data.unionation.abi_alignment = abi_alignment_so_far;
28842906
28852907 if (union_type->data.unionation.is_invalid)
2886 return;
2908 return ErrorSemanticAnalyzeFail;
28872909
28882910 bool src_have_tag = decl_node->data.container_decl.auto_enum ||
28892911 decl_node->data.container_decl.init_arg_expr != nullptr;
......@@ -2905,7 +2927,7 @@ static void resolve_union_zero_bits(CodeGen *g, TypeTableEntry *union_type) {
29052927 add_node_error(g, source_node,
29062928 buf_sprintf("%s union does not support enum tag type", qual_str));
29072929 union_type->data.unionation.is_invalid = true;
2908 return;
2930 return ErrorSemanticAnalyzeFail;
29092931 }
29102932
29112933 if (create_enum_type) {
......@@ -2970,6 +2992,8 @@ static void resolve_union_zero_bits(CodeGen *g, TypeTableEntry *union_type) {
29702992 union_type->data.unionation.gen_field_count = gen_field_index;
29712993 union_type->zero_bits = (gen_field_index == 0 && (field_count < 2 || !src_have_tag));
29722994 union_type->data.unionation.zero_bits_known = true;
2995 assert(!union_type->data.unionation.is_invalid);
2996 return ErrorNone;
29732997}
29742998
29752999static void get_fully_qualified_decl_name_internal(Buf *buf, Scope *scope, uint8_t sep) {
......@@ -3463,13 +3487,13 @@ VariableTableEntry *add_variable(CodeGen *g, AstNode *source_node, Scope *parent
34633487 variable_entry->shadowable = false;
34643488 variable_entry->mem_slot_index = SIZE_MAX;
34653489 variable_entry->src_arg_index = SIZE_MAX;
3466 variable_entry->align_bytes = get_abi_alignment(g, value->type);
34673490
34683491 assert(name);
3469
34703492 buf_init_from_buf(&variable_entry->name, name);
34713493
3472 if (value->type->id != TypeTableEntryIdInvalid) {
3494 if (!type_is_invalid(value->type)) {
3495 variable_entry->align_bytes = get_abi_alignment(g, value->type);
3496
34733497 VariableTableEntry *existing_var = find_variable(g, parent_scope, name);
34743498 if (existing_var && !existing_var->shadowable) {
34753499 ErrorMsg *msg = add_node_error(g, source_node,
......@@ -5311,13 +5335,13 @@ ConstExprValue *create_const_arg_tuple(CodeGen *g, size_t arg_index_start, size_
53115335
53125336
53135337void init_const_undefined(CodeGen *g, ConstExprValue *const_val) {
5338 Error err;
53145339 TypeTableEntry *wanted_type = const_val->type;
53155340 if (wanted_type->id == TypeTableEntryIdArray) {
53165341 const_val->special = ConstValSpecialStatic;
53175342 const_val->data.x_array.special = ConstArraySpecialUndef;
53185343 } else if (wanted_type->id == TypeTableEntryIdStruct) {
5319 ensure_complete_type(g, wanted_type);
5320 if (type_is_invalid(wanted_type)) {
5344 if ((err = ensure_complete_type(g, wanted_type))) {
53215345 return;
53225346 }
53235347
......@@ -5350,27 +5374,33 @@ ConstExprValue *create_const_vals(size_t count) {
53505374 return vals;
53515375}
53525376
5353void ensure_complete_type(CodeGen *g, TypeTableEntry *type_entry) {
5377Error ensure_complete_type(CodeGen *g, TypeTableEntry *type_entry) {
5378 if (type_is_invalid(type_entry))
5379 return ErrorSemanticAnalyzeFail;
53545380 if (type_entry->id == TypeTableEntryIdStruct) {
53555381 if (!type_entry->data.structure.complete)
5356 resolve_struct_type(g, type_entry);
5382 return resolve_struct_type(g, type_entry);
53575383 } else if (type_entry->id == TypeTableEntryIdEnum) {
53585384 if (!type_entry->data.enumeration.complete)
5359 resolve_enum_type(g, type_entry);
5385 return resolve_enum_type(g, type_entry);
53605386 } else if (type_entry->id == TypeTableEntryIdUnion) {
53615387 if (!type_entry->data.unionation.complete)
5362 resolve_union_type(g, type_entry);
5388 return resolve_union_type(g, type_entry);
53635389 }
5390 return ErrorNone;
53645391}
53655392
5366void type_ensure_zero_bits_known(CodeGen *g, TypeTableEntry *type_entry) {
5393Error type_ensure_zero_bits_known(CodeGen *g, TypeTableEntry *type_entry) {
5394 if (type_is_invalid(type_entry))
5395 return ErrorSemanticAnalyzeFail;
53675396 if (type_entry->id == TypeTableEntryIdStruct) {
5368 resolve_struct_zero_bits(g, type_entry);
5397 return resolve_struct_zero_bits(g, type_entry);
53695398 } else if (type_entry->id == TypeTableEntryIdEnum) {
5370 resolve_enum_zero_bits(g, type_entry);
5399 return resolve_enum_zero_bits(g, type_entry);
53715400 } else if (type_entry->id == TypeTableEntryIdUnion) {
5372 resolve_union_zero_bits(g, type_entry);
5401 return resolve_union_zero_bits(g, type_entry);
53735402 }
5403 return ErrorNone;
53745404}
53755405
53765406bool ir_get_var_is_comptime(VariableTableEntry *var) {
......@@ -6213,7 +6243,7 @@ LinkLib *add_link_lib(CodeGen *g, Buf *name) {
62136243}
62146244
62156245uint32_t get_abi_alignment(CodeGen *g, TypeTableEntry *type_entry) {
6216 type_ensure_zero_bits_known(g, type_entry);
6246 assertNoError(type_ensure_zero_bits_known(g, type_entry));
62176247 if (type_entry->zero_bits) return 0;
62186248
62196249 // We need to make this function work without requiring ensure_complete_type
src/analyze.hpp+4-3
......@@ -9,6 +9,7 @@
99#define ZIG_ANALYZE_HPP
1010
1111#include "all_types.hpp"
12#include "result.hpp"
1213
1314void semantic_analyze(CodeGen *g);
1415ErrorMsg *add_node_error(CodeGen *g, AstNode *node, Buf *msg);
......@@ -88,8 +89,8 @@ void init_fn_type_id(FnTypeId *fn_type_id, AstNode *proto_node, size_t param_cou
8889AstNode *get_param_decl_node(FnTableEntry *fn_entry, size_t index);
8990FnTableEntry *scope_get_fn_if_root(Scope *scope);
9091bool type_requires_comptime(TypeTableEntry *type_entry);
91void ensure_complete_type(CodeGen *g, TypeTableEntry *type_entry);
92void type_ensure_zero_bits_known(CodeGen *g, TypeTableEntry *type_entry);
92Error ATTRIBUTE_MUST_USE ensure_complete_type(CodeGen *g, TypeTableEntry *type_entry);
93Error ATTRIBUTE_MUST_USE type_ensure_zero_bits_known(CodeGen *g, TypeTableEntry *type_entry);
9394void complete_enum(CodeGen *g, TypeTableEntry *enum_type);
9495bool ir_get_var_is_comptime(VariableTableEntry *var);
9596bool const_values_equal(ConstExprValue *a, ConstExprValue *b);
......@@ -178,7 +179,7 @@ TypeTableEntryId type_id_at_index(size_t index);
178179size_t type_id_len();
179180size_t type_id_index(TypeTableEntry *entry);
180181TypeTableEntry *get_generic_fn_type(CodeGen *g, FnTypeId *fn_type_id);
181bool type_is_copyable(CodeGen *g, TypeTableEntry *type_entry);
182Result<bool> type_is_copyable(CodeGen *g, TypeTableEntry *type_entry);
182183LinkLib *create_link_lib(Buf *name);
183184bool calling_convention_does_first_arg_return(CallingConvention cc);
184185LinkLib *add_link_lib(CodeGen *codegen, Buf *lib);
src/ir.cpp+94-99
......@@ -8711,6 +8711,7 @@ static void update_errors_helper(CodeGen *g, ErrorTableEntry ***errors, size_t *
87118711}
87128712
87138713static TypeTableEntry *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, TypeTableEntry *expected_type, IrInstruction **instructions, size_t instruction_count) {
8714 Error err;
87148715 assert(instruction_count >= 1);
87158716 IrInstruction *prev_inst = instructions[0];
87168717 if (type_is_invalid(prev_inst->value.type)) {
......@@ -9172,8 +9173,7 @@ static TypeTableEntry *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_nod
91729173 if (prev_type->id == TypeTableEntryIdEnum && cur_type->id == TypeTableEntryIdUnion &&
91739174 (cur_type->data.unionation.decl_node->data.container_decl.auto_enum || cur_type->data.unionation.decl_node->data.container_decl.init_arg_expr != nullptr))
91749175 {
9175 type_ensure_zero_bits_known(ira->codegen, cur_type);
9176 if (type_is_invalid(cur_type))
9176 if ((err = type_ensure_zero_bits_known(ira->codegen, cur_type)))
91779177 return ira->codegen->builtin_types.entry_invalid;
91789178 if (cur_type->data.unionation.tag_type == prev_type) {
91799179 continue;
......@@ -9183,8 +9183,7 @@ static TypeTableEntry *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_nod
91839183 if (cur_type->id == TypeTableEntryIdEnum && prev_type->id == TypeTableEntryIdUnion &&
91849184 (prev_type->data.unionation.decl_node->data.container_decl.auto_enum || prev_type->data.unionation.decl_node->data.container_decl.init_arg_expr != nullptr))
91859185 {
9186 type_ensure_zero_bits_known(ira->codegen, prev_type);
9187 if (type_is_invalid(prev_type))
9186 if ((err = type_ensure_zero_bits_known(ira->codegen, prev_type)))
91889187 return ira->codegen->builtin_types.entry_invalid;
91899188 if (prev_type->data.unionation.tag_type == cur_type) {
91909189 prev_inst = cur_inst;
......@@ -9999,11 +9998,11 @@ static IrInstruction *ir_analyze_array_to_slice(IrAnalyze *ira, IrInstruction *s
99999998static IrInstruction *ir_analyze_enum_to_int(IrAnalyze *ira, IrInstruction *source_instr,
100009999 IrInstruction *target, TypeTableEntry *wanted_type)
1000110000{
10001 Error err;
1000210002 assert(wanted_type->id == TypeTableEntryIdInt);
1000310003
1000410004 TypeTableEntry *actual_type = target->value.type;
10005 ensure_complete_type(ira->codegen, actual_type);
10006 if (type_is_invalid(actual_type))
10005 if ((err = ensure_complete_type(ira->codegen, actual_type)))
1000710006 return ira->codegen->invalid_instruction;
1000810007
1000910008 if (wanted_type != actual_type->data.enumeration.tag_int_type) {
......@@ -10069,6 +10068,7 @@ static IrInstruction *ir_analyze_undefined_to_anything(IrAnalyze *ira, IrInstruc
1006910068static IrInstruction *ir_analyze_enum_to_union(IrAnalyze *ira, IrInstruction *source_instr,
1007010069 IrInstruction *target, TypeTableEntry *wanted_type)
1007110070{
10071 Error err;
1007210072 assert(wanted_type->id == TypeTableEntryIdUnion);
1007310073 assert(target->value.type->id == TypeTableEntryIdEnum);
1007410074
......@@ -10078,8 +10078,7 @@ static IrInstruction *ir_analyze_enum_to_union(IrAnalyze *ira, IrInstruction *so
1007810078 return ira->codegen->invalid_instruction;
1007910079 TypeUnionField *union_field = find_union_field_by_tag(wanted_type, &val->data.x_enum_tag);
1008010080 assert(union_field != nullptr);
10081 type_ensure_zero_bits_known(ira->codegen, union_field->type_entry);
10082 if (type_is_invalid(union_field->type_entry))
10081 if ((err = type_ensure_zero_bits_known(ira->codegen, union_field->type_entry)))
1008310082 return ira->codegen->invalid_instruction;
1008410083 if (!union_field->type_entry->zero_bits) {
1008510084 AstNode *field_node = wanted_type->data.unionation.decl_node->data.container_decl.fields.at(
......@@ -10169,12 +10168,12 @@ static IrInstruction *ir_analyze_widen_or_shorten(IrAnalyze *ira, IrInstruction
1016910168static IrInstruction *ir_analyze_int_to_enum(IrAnalyze *ira, IrInstruction *source_instr,
1017010169 IrInstruction *target, TypeTableEntry *wanted_type)
1017110170{
10171 Error err;
1017210172 assert(wanted_type->id == TypeTableEntryIdEnum);
1017310173
1017410174 TypeTableEntry *actual_type = target->value.type;
1017510175
10176 ensure_complete_type(ira->codegen, wanted_type);
10177 if (type_is_invalid(wanted_type))
10176 if ((err = ensure_complete_type(ira->codegen, wanted_type)))
1017810177 return ira->codegen->invalid_instruction;
1017910178
1018010179 if (actual_type != wanted_type->data.enumeration.tag_int_type) {
......@@ -10517,6 +10516,7 @@ static void report_recursive_error(IrAnalyze *ira, AstNode *source_node, ConstCa
1051710516static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_instr,
1051810517 TypeTableEntry *wanted_type, IrInstruction *value)
1051910518{
10519 Error err;
1052010520 TypeTableEntry *actual_type = value->value.type;
1052110521 AstNode *source_node = source_instr->source_node;
1052210522
......@@ -10796,8 +10796,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1079610796 if (actual_type->id == TypeTableEntryIdComptimeFloat ||
1079710797 actual_type->id == TypeTableEntryIdComptimeInt)
1079810798 {
10799 ensure_complete_type(ira->codegen, wanted_type);
10800 if (type_is_invalid(wanted_type))
10799 if ((err = ensure_complete_type(ira->codegen, wanted_type)))
1080110800 return ira->codegen->invalid_instruction;
1080210801 if (wanted_type->id == TypeTableEntryIdEnum) {
1080310802 IrInstruction *cast1 = ir_analyze_cast(ira, source_instr, wanted_type->data.enumeration.tag_int_type, value);
......@@ -10853,8 +10852,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1085310852
1085410853 // cast from union to the enum type of the union
1085510854 if (actual_type->id == TypeTableEntryIdUnion && wanted_type->id == TypeTableEntryIdEnum) {
10856 type_ensure_zero_bits_known(ira->codegen, actual_type);
10857 if (type_is_invalid(actual_type))
10855 if ((err = type_ensure_zero_bits_known(ira->codegen, actual_type)))
1085810856 return ira->codegen->invalid_instruction;
1085910857
1086010858 if (actual_type->data.unionation.tag_type == wanted_type) {
......@@ -10867,7 +10865,9 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1086710865 (wanted_type->data.unionation.decl_node->data.container_decl.auto_enum ||
1086810866 wanted_type->data.unionation.decl_node->data.container_decl.init_arg_expr != nullptr))
1086910867 {
10870 type_ensure_zero_bits_known(ira->codegen, wanted_type);
10868 if ((err = type_ensure_zero_bits_known(ira->codegen, wanted_type)))
10869 return ira->codegen->invalid_instruction;
10870
1087110871 if (wanted_type->data.unionation.tag_type == actual_type) {
1087210872 return ir_analyze_enum_to_union(ira, source_instr, value, wanted_type);
1087310873 }
......@@ -10879,7 +10879,9 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1087910879 if (union_type->data.unionation.decl_node->data.container_decl.auto_enum ||
1088010880 union_type->data.unionation.decl_node->data.container_decl.init_arg_expr != nullptr)
1088110881 {
10882 type_ensure_zero_bits_known(ira->codegen, union_type);
10882 if ((err = type_ensure_zero_bits_known(ira->codegen, union_type)))
10883 return ira->codegen->invalid_instruction;
10884
1088310885 if (union_type->data.unionation.tag_type == actual_type) {
1088410886 IrInstruction *cast1 = ir_analyze_cast(ira, source_instr, union_type, value);
1088510887 if (type_is_invalid(cast1->value.type))
......@@ -10923,8 +10925,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1092310925 types_match_const_cast_only(ira, wanted_type->data.pointer.child_type,
1092410926 actual_type, source_node, !wanted_type->data.pointer.is_const).id == ConstCastResultIdOk)
1092510927 {
10926 type_ensure_zero_bits_known(ira->codegen, actual_type);
10927 if (type_is_invalid(actual_type)) {
10928 if ((err = type_ensure_zero_bits_known(ira->codegen, actual_type))) {
1092810929 return ira->codegen->invalid_instruction;
1092910930 }
1093010931 if (!type_has_bits(actual_type)) {
......@@ -11323,6 +11324,7 @@ static bool optional_value_is_null(ConstExprValue *val) {
1132311324}
1132411325
1132511326static TypeTableEntry *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp *bin_op_instruction) {
11327 Error err;
1132611328 IrInstruction *op1 = bin_op_instruction->op1->other;
1132711329 IrInstruction *op2 = bin_op_instruction->op2->other;
1132811330 AstNode *source_node = bin_op_instruction->base.source_node;
......@@ -11458,8 +11460,7 @@ static TypeTableEntry *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp
1145811460 TypeTableEntry *resolved_type = ir_resolve_peer_types(ira, source_node, nullptr, instructions, 2);
1145911461 if (type_is_invalid(resolved_type))
1146011462 return resolved_type;
11461 type_ensure_zero_bits_known(ira->codegen, resolved_type);
11462 if (type_is_invalid(resolved_type))
11463 if ((err = type_ensure_zero_bits_known(ira->codegen, resolved_type)))
1146311464 return resolved_type;
1146411465
1146511466 bool operator_allowed;
......@@ -12406,6 +12407,7 @@ static TypeTableEntry *ir_analyze_instruction_bin_op(IrAnalyze *ira, IrInstructi
1240612407}
1240712408
1240812409static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstructionDeclVar *decl_var_instruction) {
12410 Error err;
1240912411 VariableTableEntry *var = decl_var_instruction->var;
1241012412
1241112413 IrInstruction *init_value = decl_var_instruction->init_value->other;
......@@ -12439,8 +12441,7 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc
1243912441 if (type_is_invalid(result_type)) {
1244012442 result_type = ira->codegen->builtin_types.entry_invalid;
1244112443 } else {
12442 type_ensure_zero_bits_known(ira->codegen, result_type);
12443 if (type_is_invalid(result_type)) {
12444 if ((err = type_ensure_zero_bits_known(ira->codegen, result_type))) {
1244412445 result_type = ira->codegen->builtin_types.entry_invalid;
1244512446 }
1244612447 }
......@@ -12958,6 +12959,7 @@ static VariableTableEntry *get_fn_var_by_index(FnTableEntry *fn_entry, size_t in
1295812959static IrInstruction *ir_get_var_ptr(IrAnalyze *ira, IrInstruction *instruction,
1295912960 VariableTableEntry *var)
1296012961{
12962 Error err;
1296112963 if (var->mem_slot_index != SIZE_MAX && var->owner_exec->analysis == nullptr) {
1296212964 assert(ira->codegen->errors.length != 0);
1296312965 return ira->codegen->invalid_instruction;
......@@ -13012,7 +13014,8 @@ no_mem_slot:
1301213014 instruction->scope, instruction->source_node, var);
1301313015 var_ptr_instruction->value.type = get_pointer_to_type_extra(ira->codegen, var->value->type,
1301413016 var->src_is_const, is_volatile, PtrLenSingle, var->align_bytes, 0, 0);
13015 type_ensure_zero_bits_known(ira->codegen, var->value->type);
13017 if ((err = type_ensure_zero_bits_known(ira->codegen, var->value->type)))
13018 return ira->codegen->invalid_instruction;
1301613019
1301713020 bool in_fn_scope = (scope_fn_entry(var->parent_scope) != nullptr);
1301813021 var_ptr_instruction->value.data.rh_ptr = in_fn_scope ? RuntimeHintPtrStack : RuntimeHintPtrNonStack;
......@@ -13024,6 +13027,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal
1302413027 FnTableEntry *fn_entry, TypeTableEntry *fn_type, IrInstruction *fn_ref,
1302513028 IrInstruction *first_arg_ptr, bool comptime_fn_call, FnInline fn_inline)
1302613029{
13030 Error err;
1302713031 FnTypeId *fn_type_id = &fn_type->data.fn.fn_type_id;
1302813032 size_t first_arg_1_or_0 = first_arg_ptr ? 1 : 0;
1302913033
......@@ -13388,8 +13392,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal
1338813392 inst_fn_type_id.return_type = specified_return_type;
1338913393 }
1339013394
13391 type_ensure_zero_bits_known(ira->codegen, specified_return_type);
13392 if (type_is_invalid(specified_return_type))
13395 if ((err = type_ensure_zero_bits_known(ira->codegen, specified_return_type)))
1339313396 return ira->codegen->builtin_types.entry_invalid;
1339413397
1339513398 if (type_requires_comptime(specified_return_type)) {
......@@ -13664,12 +13667,12 @@ static TypeTableEntry *ir_analyze_dereference(IrAnalyze *ira, IrInstructionUnOp
1366413667}
1366513668
1366613669static TypeTableEntry *ir_analyze_maybe(IrAnalyze *ira, IrInstructionUnOp *un_op_instruction) {
13670 Error err;
1366713671 IrInstruction *value = un_op_instruction->value->other;
1366813672 TypeTableEntry *type_entry = ir_resolve_type(ira, value);
1366913673 if (type_is_invalid(type_entry))
1367013674 return ira->codegen->builtin_types.entry_invalid;
13671 ensure_complete_type(ira->codegen, type_entry);
13672 if (type_is_invalid(type_entry))
13675 if ((err = ensure_complete_type(ira->codegen, type_entry)))
1367313676 return ira->codegen->builtin_types.entry_invalid;
1367413677
1367513678 switch (type_entry->id) {
......@@ -14023,6 +14026,7 @@ static TypeTableEntry *adjust_ptr_len(CodeGen *g, TypeTableEntry *ptr_type, PtrL
1402314026}
1402414027
1402514028static TypeTableEntry *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstructionElemPtr *elem_ptr_instruction) {
14029 Error err;
1402614030 IrInstruction *array_ptr = elem_ptr_instruction->array_ptr->other;
1402714031 if (type_is_invalid(array_ptr->value.type))
1402814032 return ira->codegen->builtin_types.entry_invalid;
......@@ -14131,8 +14135,7 @@ static TypeTableEntry *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruc
1413114135 return ira->codegen->builtin_types.entry_invalid;
1413214136
1413314137 bool safety_check_on = elem_ptr_instruction->safety_check_on;
14134 ensure_complete_type(ira->codegen, return_type->data.pointer.child_type);
14135 if (type_is_invalid(return_type->data.pointer.child_type))
14138 if ((err = ensure_complete_type(ira->codegen, return_type->data.pointer.child_type)))
1413614139 return ira->codegen->builtin_types.entry_invalid;
1413714140
1413814141 uint64_t elem_size = type_size(ira->codegen, return_type->data.pointer.child_type);
......@@ -14352,9 +14355,10 @@ static IrInstruction *ir_analyze_container_member_access_inner(IrAnalyze *ira,
1435214355static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_name,
1435314356 IrInstruction *source_instr, IrInstruction *container_ptr, TypeTableEntry *container_type)
1435414357{
14358 Error err;
14359
1435514360 TypeTableEntry *bare_type = container_ref_type(container_type);
14356 ensure_complete_type(ira->codegen, bare_type);
14357 if (type_is_invalid(bare_type))
14361 if ((err = ensure_complete_type(ira->codegen, bare_type)))
1435814362 return ira->codegen->invalid_instruction;
1435914363
1436014364 assert(container_ptr->value.type->id == TypeTableEntryIdPointer);
......@@ -14553,6 +14557,7 @@ static ErrorTableEntry *find_err_table_entry(TypeTableEntry *err_set_type, Buf *
1455314557}
1455414558
1455514559static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstructionFieldPtr *field_ptr_instruction) {
14560 Error err;
1455614561 IrInstruction *container_ptr = field_ptr_instruction->container_ptr->other;
1455714562 if (type_is_invalid(container_ptr->value.type))
1455814563 return ira->codegen->builtin_types.entry_invalid;
......@@ -14654,8 +14659,7 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru
1465414659 ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile);
1465514660 }
1465614661 if (child_type->id == TypeTableEntryIdEnum) {
14657 ensure_complete_type(ira->codegen, child_type);
14658 if (type_is_invalid(child_type))
14662 if ((err = ensure_complete_type(ira->codegen, child_type)))
1465914663 return ira->codegen->builtin_types.entry_invalid;
1466014664
1466114665 TypeEnumField *field = find_enum_type_field(child_type, field_name);
......@@ -14679,8 +14683,7 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru
1467914683 (child_type->data.unionation.decl_node->data.container_decl.init_arg_expr != nullptr ||
1468014684 child_type->data.unionation.decl_node->data.container_decl.auto_enum))
1468114685 {
14682 ensure_complete_type(ira->codegen, child_type);
14683 if (type_is_invalid(child_type))
14686 if ((err = ensure_complete_type(ira->codegen, child_type)))
1468414687 return ira->codegen->builtin_types.entry_invalid;
1468514688 TypeUnionField *field = find_union_type_field(child_type, field_name);
1468614689 if (field) {
......@@ -15257,6 +15260,7 @@ static TypeTableEntry *ir_analyze_instruction_set_float_mode(IrAnalyze *ira,
1525715260static TypeTableEntry *ir_analyze_instruction_slice_type(IrAnalyze *ira,
1525815261 IrInstructionSliceType *slice_type_instruction)
1525915262{
15263 Error err;
1526015264 uint32_t align_bytes;
1526115265 if (slice_type_instruction->align_value != nullptr) {
1526215266 if (!ir_resolve_align(ira, slice_type_instruction->align_value->other, &align_bytes))
......@@ -15306,7 +15310,8 @@ static TypeTableEntry *ir_analyze_instruction_slice_type(IrAnalyze *ira,
1530615310 case TypeTableEntryIdBoundFn:
1530715311 case TypeTableEntryIdPromise:
1530815312 {
15309 type_ensure_zero_bits_known(ira->codegen, child_type);
15313 if ((err = type_ensure_zero_bits_known(ira->codegen, child_type)))
15314 return ira->codegen->builtin_types.entry_invalid;
1531015315 TypeTableEntry *slice_ptr_type = get_pointer_to_type_extra(ira->codegen, child_type,
1531115316 is_const, is_volatile, PtrLenUnknown, align_bytes, 0, 0);
1531215317 TypeTableEntry *result_type = get_slice_type(ira->codegen, slice_ptr_type);
......@@ -15444,11 +15449,11 @@ static TypeTableEntry *ir_analyze_instruction_promise_type(IrAnalyze *ira, IrIns
1544415449static TypeTableEntry *ir_analyze_instruction_size_of(IrAnalyze *ira,
1544515450 IrInstructionSizeOf *size_of_instruction)
1544615451{
15452 Error err;
1544715453 IrInstruction *type_value = size_of_instruction->type_value->other;
1544815454 TypeTableEntry *type_entry = ir_resolve_type(ira, type_value);
1544915455
15450 ensure_complete_type(ira->codegen, type_entry);
15451 if (type_is_invalid(type_entry))
15456 if ((err = ensure_complete_type(ira->codegen, type_entry)))
1545215457 return ira->codegen->builtin_types.entry_invalid;
1545315458
1545415459 switch (type_entry->id) {
......@@ -15819,6 +15824,7 @@ static TypeTableEntry *ir_analyze_instruction_switch_br(IrAnalyze *ira,
1581915824static TypeTableEntry *ir_analyze_instruction_switch_target(IrAnalyze *ira,
1582015825 IrInstructionSwitchTarget *switch_target_instruction)
1582115826{
15827 Error err;
1582215828 IrInstruction *target_value_ptr = switch_target_instruction->target_value_ptr->other;
1582315829 if (type_is_invalid(target_value_ptr->value.type))
1582415830 return ira->codegen->builtin_types.entry_invalid;
......@@ -15845,8 +15851,7 @@ static TypeTableEntry *ir_analyze_instruction_switch_target(IrAnalyze *ira,
1584515851 if (pointee_val->special == ConstValSpecialRuntime)
1584615852 pointee_val = nullptr;
1584715853 }
15848 ensure_complete_type(ira->codegen, target_type);
15849 if (type_is_invalid(target_type))
15854 if ((err = ensure_complete_type(ira->codegen, target_type)))
1585015855 return ira->codegen->builtin_types.entry_invalid;
1585115856
1585215857 switch (target_type->id) {
......@@ -15910,8 +15915,7 @@ static TypeTableEntry *ir_analyze_instruction_switch_target(IrAnalyze *ira,
1591015915 return tag_type;
1591115916 }
1591215917 case TypeTableEntryIdEnum: {
15913 type_ensure_zero_bits_known(ira->codegen, target_type);
15914 if (type_is_invalid(target_type))
15918 if ((err = type_ensure_zero_bits_known(ira->codegen, target_type)))
1591515919 return ira->codegen->builtin_types.entry_invalid;
1591615920 if (target_type->data.enumeration.src_field_count < 2) {
1591715921 TypeEnumField *only_field = &target_type->data.enumeration.fields[0];
......@@ -16113,10 +16117,10 @@ static TypeTableEntry *ir_analyze_instruction_ref(IrAnalyze *ira, IrInstructionR
1611316117static TypeTableEntry *ir_analyze_container_init_fields_union(IrAnalyze *ira, IrInstruction *instruction,
1611416118 TypeTableEntry *container_type, size_t instr_field_count, IrInstructionContainerInitFieldsField *fields)
1611516119{
16120 Error err;
1611616121 assert(container_type->id == TypeTableEntryIdUnion);
1611716122
16118 ensure_complete_type(ira->codegen, container_type);
16119 if (type_is_invalid(container_type))
16123 if ((err = ensure_complete_type(ira->codegen, container_type)))
1612016124 return ira->codegen->builtin_types.entry_invalid;
1612116125
1612216126 if (instr_field_count != 1) {
......@@ -16145,8 +16149,7 @@ static TypeTableEntry *ir_analyze_container_init_fields_union(IrAnalyze *ira, Ir
1614516149 if (casted_field_value == ira->codegen->invalid_instruction)
1614616150 return ira->codegen->builtin_types.entry_invalid;
1614716151
16148 type_ensure_zero_bits_known(ira->codegen, casted_field_value->value.type);
16149 if (type_is_invalid(casted_field_value->value.type))
16152 if ((err = type_ensure_zero_bits_known(ira->codegen, casted_field_value->value.type)))
1615016153 return ira->codegen->builtin_types.entry_invalid;
1615116154
1615216155 bool is_comptime = ir_should_inline(ira->new_irb.exec, instruction->scope);
......@@ -16180,6 +16183,7 @@ static TypeTableEntry *ir_analyze_container_init_fields_union(IrAnalyze *ira, Ir
1618016183static TypeTableEntry *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstruction *instruction,
1618116184 TypeTableEntry *container_type, size_t instr_field_count, IrInstructionContainerInitFieldsField *fields)
1618216185{
16186 Error err;
1618316187 if (container_type->id == TypeTableEntryIdUnion) {
1618416188 return ir_analyze_container_init_fields_union(ira, instruction, container_type, instr_field_count, fields);
1618516189 }
......@@ -16190,8 +16194,7 @@ static TypeTableEntry *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstru
1619016194 return ira->codegen->builtin_types.entry_invalid;
1619116195 }
1619216196
16193 ensure_complete_type(ira->codegen, container_type);
16194 if (type_is_invalid(container_type))
16197 if ((err = ensure_complete_type(ira->codegen, container_type)))
1619516198 return ira->codegen->builtin_types.entry_invalid;
1619616199
1619716200 size_t actual_field_count = container_type->data.structure.src_field_count;
......@@ -16572,6 +16575,7 @@ static TypeTableEntry *ir_analyze_instruction_err_name(IrAnalyze *ira, IrInstruc
1657216575}
1657316576
1657416577static TypeTableEntry *ir_analyze_instruction_enum_tag_name(IrAnalyze *ira, IrInstructionTagName *instruction) {
16578 Error err;
1657516579 IrInstruction *target = instruction->target->other;
1657616580 if (type_is_invalid(target->value.type))
1657716581 return ira->codegen->builtin_types.entry_invalid;
......@@ -16579,8 +16583,7 @@ static TypeTableEntry *ir_analyze_instruction_enum_tag_name(IrAnalyze *ira, IrIn
1657916583 assert(target->value.type->id == TypeTableEntryIdEnum);
1658016584
1658116585 if (instr_is_comptime(target)) {
16582 type_ensure_zero_bits_known(ira->codegen, target->value.type);
16583 if (type_is_invalid(target->value.type))
16586 if ((err = type_ensure_zero_bits_known(ira->codegen, target->value.type)))
1658416587 return ira->codegen->builtin_types.entry_invalid;
1658516588 TypeEnumField *field = find_enum_field_by_tag(target->value.type, &target->value.data.x_bigint);
1658616589 ConstExprValue *array_val = create_const_str_lit(ira->codegen, field->name);
......@@ -16604,6 +16607,7 @@ static TypeTableEntry *ir_analyze_instruction_enum_tag_name(IrAnalyze *ira, IrIn
1660416607static TypeTableEntry *ir_analyze_instruction_field_parent_ptr(IrAnalyze *ira,
1660516608 IrInstructionFieldParentPtr *instruction)
1660616609{
16610 Error err;
1660716611 IrInstruction *type_value = instruction->type_value->other;
1660816612 TypeTableEntry *container_type = ir_resolve_type(ira, type_value);
1660916613 if (type_is_invalid(container_type))
......@@ -16624,8 +16628,7 @@ static TypeTableEntry *ir_analyze_instruction_field_parent_ptr(IrAnalyze *ira,
1662416628 return ira->codegen->builtin_types.entry_invalid;
1662516629 }
1662616630
16627 ensure_complete_type(ira->codegen, container_type);
16628 if (type_is_invalid(container_type))
16631 if ((err = ensure_complete_type(ira->codegen, container_type)))
1662916632 return ira->codegen->builtin_types.entry_invalid;
1663016633
1663116634 TypeStructField *field = find_struct_type_field(container_type, field_name);
......@@ -16697,13 +16700,13 @@ static TypeTableEntry *ir_analyze_instruction_field_parent_ptr(IrAnalyze *ira,
1669716700static TypeTableEntry *ir_analyze_instruction_offset_of(IrAnalyze *ira,
1669816701 IrInstructionOffsetOf *instruction)
1669916702{
16703 Error err;
1670016704 IrInstruction *type_value = instruction->type_value->other;
1670116705 TypeTableEntry *container_type = ir_resolve_type(ira, type_value);
1670216706 if (type_is_invalid(container_type))
1670316707 return ira->codegen->builtin_types.entry_invalid;
1670416708
16705 ensure_complete_type(ira->codegen, container_type);
16706 if (type_is_invalid(container_type))
16709 if ((err = ensure_complete_type(ira->codegen, container_type)))
1670716710 return ira->codegen->builtin_types.entry_invalid;
1670816711
1670916712 IrInstruction *field_name_value = instruction->field_name->other;
......@@ -16750,6 +16753,7 @@ static void ensure_field_index(TypeTableEntry *type, const char *field_name, siz
1675016753
1675116754static TypeTableEntry *ir_type_info_get_type(IrAnalyze *ira, const char *type_name, TypeTableEntry *root = nullptr)
1675216755{
16756 Error err;
1675316757 static ConstExprValue *type_info_var = nullptr;
1675416758 static TypeTableEntry *type_info_type = nullptr;
1675516759 if (type_info_var == nullptr)
......@@ -16757,8 +16761,7 @@ static TypeTableEntry *ir_type_info_get_type(IrAnalyze *ira, const char *type_na
1675716761 type_info_var = get_builtin_value(ira->codegen, "TypeInfo");
1675816762 assert(type_info_var->type->id == TypeTableEntryIdMetaType);
1675916763
16760 ensure_complete_type(ira->codegen, type_info_var->data.x_type);
16761 if (type_is_invalid(type_info_var->data.x_type))
16764 if ((err = ensure_complete_type(ira->codegen, type_info_var->data.x_type)))
1676216765 return ira->codegen->builtin_types.entry_invalid;
1676316766
1676416767 type_info_type = type_info_var->data.x_type;
......@@ -16785,8 +16788,7 @@ static TypeTableEntry *ir_type_info_get_type(IrAnalyze *ira, const char *type_na
1678516788
1678616789 VariableTableEntry *var = tld->var;
1678716790
16788 ensure_complete_type(ira->codegen, var->value->type);
16789 if (type_is_invalid(var->value->type))
16791 if ((err = ensure_complete_type(ira->codegen, var->value->type)))
1679016792 return ira->codegen->builtin_types.entry_invalid;
1679116793 assert(var->value->type->id == TypeTableEntryIdMetaType);
1679216794 return var->value->data.x_type;
......@@ -16794,9 +16796,9 @@ static TypeTableEntry *ir_type_info_get_type(IrAnalyze *ira, const char *type_na
1679416796
1679516797static bool ir_make_type_info_defs(IrAnalyze *ira, ConstExprValue *out_val, ScopeDecls *decls_scope)
1679616798{
16799 Error err;
1679716800 TypeTableEntry *type_info_definition_type = ir_type_info_get_type(ira, "Definition");
16798 ensure_complete_type(ira->codegen, type_info_definition_type);
16799 if (type_is_invalid(type_info_definition_type))
16801 if ((err = ensure_complete_type(ira->codegen, type_info_definition_type)))
1680016802 return false;
1680116803
1680216804 ensure_field_index(type_info_definition_type, "name", 0);
......@@ -16804,18 +16806,15 @@ static bool ir_make_type_info_defs(IrAnalyze *ira, ConstExprValue *out_val, Scop
1680416806 ensure_field_index(type_info_definition_type, "data", 2);
1680516807
1680616808 TypeTableEntry *type_info_definition_data_type = ir_type_info_get_type(ira, "Data", type_info_definition_type);
16807 ensure_complete_type(ira->codegen, type_info_definition_data_type);
16808 if (type_is_invalid(type_info_definition_data_type))
16809 if ((err = ensure_complete_type(ira->codegen, type_info_definition_data_type)))
1680916810 return false;
1681016811
1681116812 TypeTableEntry *type_info_fn_def_type = ir_type_info_get_type(ira, "FnDef", type_info_definition_data_type);
16812 ensure_complete_type(ira->codegen, type_info_fn_def_type);
16813 if (type_is_invalid(type_info_fn_def_type))
16813 if ((err = ensure_complete_type(ira->codegen, type_info_fn_def_type)))
1681416814 return false;
1681516815
1681616816 TypeTableEntry *type_info_fn_def_inline_type = ir_type_info_get_type(ira, "Inline", type_info_fn_def_type);
16817 ensure_complete_type(ira->codegen, type_info_fn_def_inline_type);
16818 if (type_is_invalid(type_info_fn_def_inline_type))
16817 if ((err = ensure_complete_type(ira->codegen, type_info_fn_def_inline_type)))
1681916818 return false;
1682016819
1682116820 // Loop through our definitions once to figure out how many definitions we will generate info for.
......@@ -16895,8 +16894,7 @@ static bool ir_make_type_info_defs(IrAnalyze *ira, ConstExprValue *out_val, Scop
1689516894 case TldIdVar:
1689616895 {
1689716896 VariableTableEntry *var = ((TldVar *)curr_entry->value)->var;
16898 ensure_complete_type(ira->codegen, var->value->type);
16899 if (type_is_invalid(var->value->type))
16897 if ((err = ensure_complete_type(ira->codegen, var->value->type)))
1690016898 return false;
1690116899
1690216900 if (var->value->type->id == TypeTableEntryIdMetaType)
......@@ -17027,8 +17025,7 @@ static bool ir_make_type_info_defs(IrAnalyze *ira, ConstExprValue *out_val, Scop
1702717025 case TldIdContainer:
1702817026 {
1702917027 TypeTableEntry *type_entry = ((TldContainer *)curr_entry->value)->type_entry;
17030 ensure_complete_type(ira->codegen, type_entry);
17031 if (type_is_invalid(type_entry))
17028 if ((err = ensure_complete_type(ira->codegen, type_entry)))
1703217029 return false;
1703317030
1703417031 // This is a type.
......@@ -17055,11 +17052,11 @@ static bool ir_make_type_info_defs(IrAnalyze *ira, ConstExprValue *out_val, Scop
1705517052}
1705617053
1705717054static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *type_entry) {
17055 Error err;
1705817056 assert(type_entry != nullptr);
1705917057 assert(!type_is_invalid(type_entry));
1706017058
17061 ensure_complete_type(ira->codegen, type_entry);
17062 if (type_is_invalid(type_entry))
17059 if ((err = ensure_complete_type(ira->codegen, type_entry)))
1706317060 return nullptr;
1706417061
1706517062 const auto make_enum_field_val = [ira](ConstExprValue *enum_field_val, TypeEnumField *enum_field,
......@@ -17093,8 +17090,7 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
1709317090 }
1709417091
1709517092 TypeTableEntry *type_info_pointer_type = ir_type_info_get_type(ira, "Pointer");
17096 ensure_complete_type(ira->codegen, type_info_pointer_type);
17097 assert(!type_is_invalid(type_info_pointer_type));
17093 assertNoError(ensure_complete_type(ira->codegen, type_info_pointer_type));
1709817094
1709917095 ConstExprValue *result = create_const_vals(1);
1710017096 result->special = ConstValSpecialStatic;
......@@ -17106,8 +17102,7 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
1710617102 // size: Size
1710717103 ensure_field_index(result->type, "size", 0);
1710817104 TypeTableEntry *type_info_pointer_size_type = ir_type_info_get_type(ira, "Size", type_info_pointer_type);
17109 ensure_complete_type(ira->codegen, type_info_pointer_size_type);
17110 assert(!type_is_invalid(type_info_pointer_size_type));
17105 assertNoError(ensure_complete_type(ira->codegen, type_info_pointer_size_type));
1711117106 fields[0].special = ConstValSpecialStatic;
1711217107 fields[0].type = type_info_pointer_size_type;
1711317108 bigint_init_unsigned(&fields[0].data.x_enum_tag, size_enum_index);
......@@ -18896,13 +18891,13 @@ static TypeTableEntry *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructio
1889618891}
1889718892
1889818893static TypeTableEntry *ir_analyze_instruction_member_count(IrAnalyze *ira, IrInstructionMemberCount *instruction) {
18894 Error err;
1889918895 IrInstruction *container = instruction->container->other;
1890018896 if (type_is_invalid(container->value.type))
1890118897 return ira->codegen->builtin_types.entry_invalid;
1890218898 TypeTableEntry *container_type = ir_resolve_type(ira, container);
1890318899
18904 ensure_complete_type(ira->codegen, container_type);
18905 if (type_is_invalid(container_type))
18900 if ((err = ensure_complete_type(ira->codegen, container_type)))
1890618901 return ira->codegen->builtin_types.entry_invalid;
1890718902
1890818903 uint64_t result;
......@@ -18934,13 +18929,13 @@ static TypeTableEntry *ir_analyze_instruction_member_count(IrAnalyze *ira, IrIns
1893418929}
1893518930
1893618931static TypeTableEntry *ir_analyze_instruction_member_type(IrAnalyze *ira, IrInstructionMemberType *instruction) {
18932 Error err;
1893718933 IrInstruction *container_type_value = instruction->container_type->other;
1893818934 TypeTableEntry *container_type = ir_resolve_type(ira, container_type_value);
1893918935 if (type_is_invalid(container_type))
1894018936 return ira->codegen->builtin_types.entry_invalid;
1894118937
18942 ensure_complete_type(ira->codegen, container_type);
18943 if (type_is_invalid(container_type))
18938 if ((err = ensure_complete_type(ira->codegen, container_type)))
1894418939 return ira->codegen->builtin_types.entry_invalid;
1894518940
1894618941
......@@ -18981,13 +18976,13 @@ static TypeTableEntry *ir_analyze_instruction_member_type(IrAnalyze *ira, IrInst
1898118976}
1898218977
1898318978static TypeTableEntry *ir_analyze_instruction_member_name(IrAnalyze *ira, IrInstructionMemberName *instruction) {
18979 Error err;
1898418980 IrInstruction *container_type_value = instruction->container_type->other;
1898518981 TypeTableEntry *container_type = ir_resolve_type(ira, container_type_value);
1898618982 if (type_is_invalid(container_type))
1898718983 return ira->codegen->builtin_types.entry_invalid;
1898818984
18989 ensure_complete_type(ira->codegen, container_type);
18990 if (type_is_invalid(container_type))
18985 if ((err = ensure_complete_type(ira->codegen, container_type)))
1899118986 return ira->codegen->builtin_types.entry_invalid;
1899218987
1899318988 uint64_t member_index;
......@@ -19068,13 +19063,13 @@ static TypeTableEntry *ir_analyze_instruction_handle(IrAnalyze *ira, IrInstructi
1906819063}
1906919064
1907019065static TypeTableEntry *ir_analyze_instruction_align_of(IrAnalyze *ira, IrInstructionAlignOf *instruction) {
19066 Error err;
1907119067 IrInstruction *type_value = instruction->type_value->other;
1907219068 if (type_is_invalid(type_value->value.type))
1907319069 return ira->codegen->builtin_types.entry_invalid;
1907419070 TypeTableEntry *type_entry = ir_resolve_type(ira, type_value);
1907519071
19076 type_ensure_zero_bits_known(ira->codegen, type_entry);
19077 if (type_is_invalid(type_entry))
19072 if ((err = type_ensure_zero_bits_known(ira->codegen, type_entry)))
1907819073 return ira->codegen->builtin_types.entry_invalid;
1907919074
1908019075 switch (type_entry->id) {
......@@ -19930,6 +19925,7 @@ static void buf_read_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue
1993019925}
1993119926
1993219927static TypeTableEntry *ir_analyze_instruction_bit_cast(IrAnalyze *ira, IrInstructionBitCast *instruction) {
19928 Error err;
1993319929 IrInstruction *dest_type_value = instruction->dest_type->other;
1993419930 TypeTableEntry *dest_type = ir_resolve_type(ira, dest_type_value);
1993519931 if (type_is_invalid(dest_type))
......@@ -19940,12 +19936,10 @@ static TypeTableEntry *ir_analyze_instruction_bit_cast(IrAnalyze *ira, IrInstruc
1994019936 if (type_is_invalid(src_type))
1994119937 return ira->codegen->builtin_types.entry_invalid;
1994219938
19943 ensure_complete_type(ira->codegen, dest_type);
19944 if (type_is_invalid(dest_type))
19939 if ((err = ensure_complete_type(ira->codegen, dest_type)))
1994519940 return ira->codegen->builtin_types.entry_invalid;
1994619941
19947 ensure_complete_type(ira->codegen, src_type);
19948 if (type_is_invalid(src_type))
19942 if ((err = ensure_complete_type(ira->codegen, src_type)))
1994919943 return ira->codegen->builtin_types.entry_invalid;
1995019944
1995119945 if (get_codegen_ptr_type(src_type) != nullptr) {
......@@ -20031,6 +20025,7 @@ static TypeTableEntry *ir_analyze_instruction_bit_cast(IrAnalyze *ira, IrInstruc
2003120025}
2003220026
2003320027static TypeTableEntry *ir_analyze_instruction_int_to_ptr(IrAnalyze *ira, IrInstructionIntToPtr *instruction) {
20028 Error err;
2003420029 IrInstruction *dest_type_value = instruction->dest_type->other;
2003520030 TypeTableEntry *dest_type = ir_resolve_type(ira, dest_type_value);
2003620031 if (type_is_invalid(dest_type))
......@@ -20041,7 +20036,8 @@ static TypeTableEntry *ir_analyze_instruction_int_to_ptr(IrAnalyze *ira, IrInstr
2004120036 return ira->codegen->builtin_types.entry_invalid;
2004220037 }
2004320038
20044 type_ensure_zero_bits_known(ira->codegen, dest_type);
20039 if ((err = type_ensure_zero_bits_known(ira->codegen, dest_type)))
20040 return ira->codegen->builtin_types.entry_invalid;
2004520041 if (!type_has_bits(dest_type)) {
2004620042 ir_add_error(ira, dest_type_value,
2004720043 buf_sprintf("type '%s' has 0 bits and cannot store information", buf_ptr(&dest_type->name)));
......@@ -20174,6 +20170,7 @@ static TypeTableEntry *ir_analyze_instruction_ptr_to_int(IrAnalyze *ira, IrInstr
2017420170}
2017520171
2017620172static TypeTableEntry *ir_analyze_instruction_ptr_type(IrAnalyze *ira, IrInstructionPtrType *instruction) {
20173 Error err;
2017720174 TypeTableEntry *child_type = ir_resolve_type(ira, instruction->child_type->other);
2017820175 if (type_is_invalid(child_type))
2017920176 return ira->codegen->builtin_types.entry_invalid;
......@@ -20191,8 +20188,7 @@ static TypeTableEntry *ir_analyze_instruction_ptr_type(IrAnalyze *ira, IrInstruc
2019120188 if (!ir_resolve_align(ira, instruction->align_value->other, &align_bytes))
2019220189 return ira->codegen->builtin_types.entry_invalid;
2019320190 } else {
20194 type_ensure_zero_bits_known(ira->codegen, child_type);
20195 if (type_is_invalid(child_type))
20191 if ((err = type_ensure_zero_bits_known(ira->codegen, child_type)))
2019620192 return ira->codegen->builtin_types.entry_invalid;
2019720193 align_bytes = get_abi_alignment(ira->codegen, child_type);
2019820194 }
......@@ -20312,22 +20308,21 @@ static TypeTableEntry *ir_analyze_instruction_arg_type(IrAnalyze *ira, IrInstruc
2031220308}
2031320309
2031420310static TypeTableEntry *ir_analyze_instruction_tag_type(IrAnalyze *ira, IrInstructionTagType *instruction) {
20311 Error err;
2031520312 IrInstruction *target_inst = instruction->target->other;
2031620313 TypeTableEntry *enum_type = ir_resolve_type(ira, target_inst);
2031720314 if (type_is_invalid(enum_type))
2031820315 return ira->codegen->builtin_types.entry_invalid;
2031920316
2032020317 if (enum_type->id == TypeTableEntryIdEnum) {
20321 ensure_complete_type(ira->codegen, enum_type);
20322 if (type_is_invalid(enum_type))
20318 if ((err = ensure_complete_type(ira->codegen, enum_type)))
2032320319 return ira->codegen->builtin_types.entry_invalid;
2032420320
2032520321 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
2032620322 out_val->data.x_type = enum_type->data.enumeration.tag_int_type;
2032720323 return ira->codegen->builtin_types.entry_type;
2032820324 } else if (enum_type->id == TypeTableEntryIdUnion) {
20329 ensure_complete_type(ira->codegen, enum_type);
20330 if (type_is_invalid(enum_type))
20325 if ((err = ensure_complete_type(ira->codegen, enum_type)))
2033120326 return ira->codegen->builtin_types.entry_invalid;
2033220327
2033320328 AstNode *decl_node = enum_type->data.unionation.decl_node;
......@@ -20830,6 +20825,7 @@ static TypeTableEntry *ir_analyze_instruction_sqrt(IrAnalyze *ira, IrInstruction
2083020825}
2083120826
2083220827static TypeTableEntry *ir_analyze_instruction_enum_to_int(IrAnalyze *ira, IrInstructionEnumToInt *instruction) {
20828 Error err;
2083320829 IrInstruction *target = instruction->target->other;
2083420830 if (type_is_invalid(target->value.type))
2083520831 return ira->codegen->builtin_types.entry_invalid;
......@@ -20840,8 +20836,7 @@ static TypeTableEntry *ir_analyze_instruction_enum_to_int(IrAnalyze *ira, IrInst
2084020836 return ira->codegen->builtin_types.entry_invalid;
2084120837 }
2084220838
20843 type_ensure_zero_bits_known(ira->codegen, target->value.type);
20844 if (type_is_invalid(target->value.type))
20839 if ((err = type_ensure_zero_bits_known(ira->codegen, target->value.type)))
2084520840 return ira->codegen->builtin_types.entry_invalid;
2084620841
2084720842 TypeTableEntry *tag_type = target->value.type->data.enumeration.tag_int_type;
......@@ -20852,6 +20847,7 @@ static TypeTableEntry *ir_analyze_instruction_enum_to_int(IrAnalyze *ira, IrInst
2085220847}
2085320848
2085420849static TypeTableEntry *ir_analyze_instruction_int_to_enum(IrAnalyze *ira, IrInstructionIntToEnum *instruction) {
20850 Error err;
2085520851 IrInstruction *dest_type_value = instruction->dest_type->other;
2085620852 TypeTableEntry *dest_type = ir_resolve_type(ira, dest_type_value);
2085720853 if (type_is_invalid(dest_type))
......@@ -20863,8 +20859,7 @@ static TypeTableEntry *ir_analyze_instruction_int_to_enum(IrAnalyze *ira, IrInst
2086320859 return ira->codegen->builtin_types.entry_invalid;
2086420860 }
2086520861
20866 type_ensure_zero_bits_known(ira->codegen, dest_type);
20867 if (type_is_invalid(dest_type))
20862 if ((err = type_ensure_zero_bits_known(ira->codegen, dest_type)))
2086820863 return ira->codegen->builtin_types.entry_invalid;
2086920864
2087020865 TypeTableEntry *tag_type = dest_type->data.enumeration.tag_int_type;
src/result.hpp created+36
......@@ -0,0 +1,36 @@
1/*
2 * Copyright (c) 2018 Andrew Kelley
3 *
4 * This file is part of zig, which is MIT licensed.
5 * See http://opensource.org/licenses/MIT
6 */
7
8#ifndef ZIG_RESULT_HPP
9#define ZIG_RESULT_HPP
10
11#include "error.hpp"
12
13#include <assert.h>
14
15static inline void assertNoError(Error err) {
16 assert(err == ErrorNone);
17}
18
19template<typename T>
20struct Result {
21 T data;
22 Error err;
23
24 Result(T x) : data(x), err(ErrorNone) {}
25
26 Result(Error err) : err(err) {
27 assert(err != ErrorNone);
28 }
29
30 T unwrap() {
31 assert(err == ErrorNone);
32 return data;
33 }
34};
35
36#endif
src/util.hpp+2
......@@ -21,6 +21,7 @@
2121#define ATTRIBUTE_PRINTF(a, b)
2222#define ATTRIBUTE_RETURNS_NOALIAS __declspec(restrict)
2323#define ATTRIBUTE_NORETURN __declspec(noreturn)
24#define ATTRIBUTE_MUST_USE
2425
2526#else
2627
......@@ -28,6 +29,7 @@
2829#define ATTRIBUTE_PRINTF(a, b) __attribute__((format(printf, a, b)))
2930#define ATTRIBUTE_RETURNS_NOALIAS __attribute__((__malloc__))
3031#define ATTRIBUTE_NORETURN __attribute__((noreturn))
32#define ATTRIBUTE_MUST_USE __attribute__((warn_unused_result))
3133
3234#endif
3335
std/c/darwin.zig+5-347
......@@ -1,3 +1,5 @@
1const macho = @import("../macho.zig");
2
13extern "c" fn __error() *c_int;
24pub extern "c" fn _NSGetExecutablePath(buf: [*]u8, bufsize: *u32) c_int;
35pub extern "c" fn _dyld_get_image_header(image_index: u32) ?*mach_header;
......@@ -40,6 +42,9 @@ pub extern "c" fn socket(domain: c_int, type: c_int, protocol: c_int) c_int;
4042/// absolute as the header is not part of any section.
4143pub extern "c" var _mh_execute_header: if (@sizeOf(usize) == 8) mach_header_64 else mach_header;
4244
45pub const mach_header_64 = macho.mach_header_64;
46pub const mach_header = macho.mach_header;
47
4348pub use @import("../os/darwin/errno.zig");
4449
4550pub const _errno = __error;
......@@ -146,353 +151,6 @@ pub const Kevent = extern struct {
146151 udata: usize,
147152};
148153
149pub const mach_header = extern struct {
150 magic: u32,
151 cputype: cpu_type_t,
152 cpusubtype: cpu_subtype_t,
153 filetype: u32,
154 ncmds: u32,
155 sizeofcmds: u32,
156 flags: u32,
157};
158
159pub const mach_header_64 = extern struct {
160 magic: u32,
161 cputype: cpu_type_t,
162 cpusubtype: cpu_subtype_t,
163 filetype: u32,
164 ncmds: u32,
165 sizeofcmds: u32,
166 flags: u32,
167 reserved: u32,
168};
169
170pub const load_command = extern struct {
171 cmd: u32,
172 cmdsize: u32,
173};
174
175
176/// The symtab_command contains the offsets and sizes of the link-edit 4.3BSD
177/// "stab" style symbol table information as described in the header files
178/// <nlist.h> and <stab.h>.
179pub const symtab_command = extern struct {
180 cmd: u32, /// LC_SYMTAB
181 cmdsize: u32, /// sizeof(struct symtab_command)
182 symoff: u32, /// symbol table offset
183 nsyms: u32, /// number of symbol table entries
184 stroff: u32, /// string table offset
185 strsize: u32, /// string table size in bytes
186};
187
188/// The linkedit_data_command contains the offsets and sizes of a blob
189/// of data in the __LINKEDIT segment.
190const linkedit_data_command = extern struct {
191 cmd: u32,/// LC_CODE_SIGNATURE, LC_SEGMENT_SPLIT_INFO, LC_FUNCTION_STARTS, LC_DATA_IN_CODE, LC_DYLIB_CODE_SIGN_DRS or LC_LINKER_OPTIMIZATION_HINT.
192 cmdsize: u32, /// sizeof(struct linkedit_data_command)
193 dataoff: u32 , /// file offset of data in __LINKEDIT segment
194 datasize: u32 , /// file size of data in __LINKEDIT segment
195};
196
197/// The segment load command indicates that a part of this file is to be
198/// mapped into the task's address space. The size of this segment in memory,
199/// vmsize, maybe equal to or larger than the amount to map from this file,
200/// filesize. The file is mapped starting at fileoff to the beginning of
201/// the segment in memory, vmaddr. The rest of the memory of the segment,
202/// if any, is allocated zero fill on demand. The segment's maximum virtual
203/// memory protection and initial virtual memory protection are specified
204/// by the maxprot and initprot fields. If the segment has sections then the
205/// section structures directly follow the segment command and their size is
206/// reflected in cmdsize.
207pub const segment_command = extern struct {
208 cmd: u32,/// LC_SEGMENT
209 cmdsize: u32,/// includes sizeof section structs
210 segname: [16]u8,/// segment name
211 vmaddr: u32,/// memory address of this segment
212 vmsize: u32,/// memory size of this segment
213 fileoff: u32,/// file offset of this segment
214 filesize: u32,/// amount to map from the file
215 maxprot: vm_prot_t,/// maximum VM protection
216 initprot: vm_prot_t,/// initial VM protection
217 nsects: u32,/// number of sections in segment
218 flags: u32,
219};
220
221/// The 64-bit segment load command indicates that a part of this file is to be
222/// mapped into a 64-bit task's address space. If the 64-bit segment has
223/// sections then section_64 structures directly follow the 64-bit segment
224/// command and their size is reflected in cmdsize.
225pub const segment_command_64 = extern struct {
226 cmd: u32, /// LC_SEGMENT_64
227 cmdsize: u32, /// includes sizeof section_64 structs
228 segname: [16]u8, /// segment name
229 vmaddr: u64, /// memory address of this segment
230 vmsize: u64, /// memory size of this segment
231 fileoff: u64, /// file offset of this segment
232 filesize: u64, /// amount to map from the file
233 maxprot: vm_prot_t, /// maximum VM protection
234 initprot: vm_prot_t, /// initial VM protection
235 nsects: u32, /// number of sections in segment
236 flags: u32,
237};
238
239/// A segment is made up of zero or more sections. Non-MH_OBJECT files have
240/// all of their segments with the proper sections in each, and padded to the
241/// specified segment alignment when produced by the link editor. The first
242/// segment of a MH_EXECUTE and MH_FVMLIB format file contains the mach_header
243/// and load commands of the object file before its first section. The zero
244/// fill sections are always last in their segment (in all formats). This
245/// allows the zeroed segment padding to be mapped into memory where zero fill
246/// sections might be. The gigabyte zero fill sections, those with the section
247/// type S_GB_ZEROFILL, can only be in a segment with sections of this type.
248/// These segments are then placed after all other segments.
249///
250/// The MH_OBJECT format has all of its sections in one segment for
251/// compactness. There is no padding to a specified segment boundary and the
252/// mach_header and load commands are not part of the segment.
253///
254/// Sections with the same section name, sectname, going into the same segment,
255/// segname, are combined by the link editor. The resulting section is aligned
256/// to the maximum alignment of the combined sections and is the new section's
257/// alignment. The combined sections are aligned to their original alignment in
258/// the combined section. Any padded bytes to get the specified alignment are
259/// zeroed.
260///
261/// The format of the relocation entries referenced by the reloff and nreloc
262/// fields of the section structure for mach object files is described in the
263/// header file <reloc.h>.
264pub const @"section" = extern struct {
265 sectname: [16]u8, /// name of this section
266 segname: [16]u8, /// segment this section goes in
267 addr: u32, /// memory address of this section
268 size: u32, /// size in bytes of this section
269 offset: u32, /// file offset of this section
270 @"align": u32, /// section alignment (power of 2)
271 reloff: u32, /// file offset of relocation entries
272 nreloc: u32, /// number of relocation entries
273 flags: u32, /// flags (section type and attributes
274 reserved1: u32, /// reserved (for offset or index)
275 reserved2: u32, /// reserved (for count or sizeof)
276};
277
278pub const section_64 = extern struct {
279 sectname: [16]u8, /// name of this section
280 segname: [16]u8, /// segment this section goes in
281 addr: u64, /// memory address of this section
282 size: u64, /// size in bytes of this section
283 offset: u32, /// file offset of this section
284 @"align": u32, /// section alignment (power of 2)
285 reloff: u32, /// file offset of relocation entries
286 nreloc: u32, /// number of relocation entries
287 flags: u32, /// flags (section type and attributes
288 reserved1: u32, /// reserved (for offset or index)
289 reserved2: u32, /// reserved (for count or sizeof)
290 reserved3: u32, /// reserved
291};
292
293pub const nlist = extern struct {
294 n_strx: u32,
295 n_type: u8,
296 n_sect: u8,
297 n_desc: i16,
298 n_value: u32,
299};
300
301pub const nlist_64 = extern struct {
302 n_strx: u32,
303 n_type: u8,
304 n_sect: u8,
305 n_desc: u16,
306 n_value: u64,
307};
308
309/// After MacOS X 10.1 when a new load command is added that is required to be
310/// understood by the dynamic linker for the image to execute properly the
311/// LC_REQ_DYLD bit will be or'ed into the load command constant. If the dynamic
312/// linker sees such a load command it it does not understand will issue a
313/// "unknown load command required for execution" error and refuse to use the
314/// image. Other load commands without this bit that are not understood will
315/// simply be ignored.
316pub const LC_REQ_DYLD = 0x80000000;
317
318pub const LC_SEGMENT = 0x1; /// segment of this file to be mapped
319pub const LC_SYMTAB = 0x2; /// link-edit stab symbol table info
320pub const LC_SYMSEG = 0x3; /// link-edit gdb symbol table info (obsolete)
321pub const LC_THREAD = 0x4; /// thread
322pub const LC_UNIXTHREAD = 0x5; /// unix thread (includes a stack)
323pub const LC_LOADFVMLIB = 0x6; /// load a specified fixed VM shared library
324pub const LC_IDFVMLIB = 0x7; /// fixed VM shared library identification
325pub const LC_IDENT = 0x8; /// object identification info (obsolete)
326pub const LC_FVMFILE = 0x9; /// fixed VM file inclusion (internal use)
327pub const LC_PREPAGE = 0xa; /// prepage command (internal use)
328pub const LC_DYSYMTAB = 0xb; /// dynamic link-edit symbol table info
329pub const LC_LOAD_DYLIB = 0xc; /// load a dynamically linked shared library
330pub const LC_ID_DYLIB = 0xd; /// dynamically linked shared lib ident
331pub const LC_LOAD_DYLINKER = 0xe; /// load a dynamic linker
332pub const LC_ID_DYLINKER = 0xf; /// dynamic linker identification
333pub const LC_PREBOUND_DYLIB = 0x10; /// modules prebound for a dynamically
334pub const LC_ROUTINES = 0x11; /// image routines
335pub const LC_SUB_FRAMEWORK = 0x12; /// sub framework
336pub const LC_SUB_UMBRELLA = 0x13; /// sub umbrella
337pub const LC_SUB_CLIENT = 0x14; /// sub client
338pub const LC_SUB_LIBRARY = 0x15; /// sub library
339pub const LC_TWOLEVEL_HINTS = 0x16; /// two-level namespace lookup hints
340pub const LC_PREBIND_CKSUM = 0x17; /// prebind checksum
341
342/// load a dynamically linked shared library that is allowed to be missing
343/// (all symbols are weak imported).
344pub const LC_LOAD_WEAK_DYLIB = (0x18 | LC_REQ_DYLD);
345
346pub const LC_SEGMENT_64 = 0x19; /// 64-bit segment of this file to be mapped
347pub const LC_ROUTINES_64 = 0x1a; /// 64-bit image routines
348pub const LC_UUID = 0x1b; /// the uuid
349pub const LC_RPATH = (0x1c | LC_REQ_DYLD); /// runpath additions
350pub const LC_CODE_SIGNATURE = 0x1d; /// local of code signature
351pub const LC_SEGMENT_SPLIT_INFO = 0x1e; /// local of info to split segments
352pub const LC_REEXPORT_DYLIB = (0x1f | LC_REQ_DYLD); /// load and re-export dylib
353pub const LC_LAZY_LOAD_DYLIB = 0x20; /// delay load of dylib until first use
354pub const LC_ENCRYPTION_INFO = 0x21; /// encrypted segment information
355pub const LC_DYLD_INFO = 0x22; /// compressed dyld information
356pub const LC_DYLD_INFO_ONLY = (0x22|LC_REQ_DYLD); /// compressed dyld information only
357pub const LC_LOAD_UPWARD_DYLIB = (0x23 | LC_REQ_DYLD); /// load upward dylib
358pub const LC_VERSION_MIN_MACOSX = 0x24; /// build for MacOSX min OS version
359pub const LC_VERSION_MIN_IPHONEOS = 0x25; /// build for iPhoneOS min OS version
360pub const LC_FUNCTION_STARTS = 0x26; /// compressed table of function start addresses
361pub const LC_DYLD_ENVIRONMENT = 0x27; /// string for dyld to treat like environment variable
362pub const LC_MAIN = (0x28|LC_REQ_DYLD); /// replacement for LC_UNIXTHREAD
363pub const LC_DATA_IN_CODE = 0x29; /// table of non-instructions in __text
364pub const LC_SOURCE_VERSION = 0x2A; /// source version used to build binary
365pub const LC_DYLIB_CODE_SIGN_DRS = 0x2B; /// Code signing DRs copied from linked dylibs
366pub const LC_ENCRYPTION_INFO_64 = 0x2C; /// 64-bit encrypted segment information
367pub const LC_LINKER_OPTION = 0x2D; /// linker options in MH_OBJECT files
368pub const LC_LINKER_OPTIMIZATION_HINT = 0x2E; /// optimization hints in MH_OBJECT files
369pub const LC_VERSION_MIN_TVOS = 0x2F; /// build for AppleTV min OS version
370pub const LC_VERSION_MIN_WATCHOS = 0x30; /// build for Watch min OS version
371pub const LC_NOTE = 0x31; /// arbitrary data included within a Mach-O file
372pub const LC_BUILD_VERSION = 0x32; /// build for platform min OS version
373
374pub const MH_MAGIC = 0xfeedface; /// the mach magic number
375pub const MH_CIGAM = 0xcefaedfe; /// NXSwapInt(MH_MAGIC)
376
377pub const MH_MAGIC_64 = 0xfeedfacf; /// the 64-bit mach magic number
378pub const MH_CIGAM_64 = 0xcffaedfe; /// NXSwapInt(MH_MAGIC_64)
379
380pub const MH_OBJECT = 0x1; /// relocatable object file
381pub const MH_EXECUTE = 0x2; /// demand paged executable file
382pub const MH_FVMLIB = 0x3; /// fixed VM shared library file
383pub const MH_CORE = 0x4; /// core file
384pub const MH_PRELOAD = 0x5; /// preloaded executable file
385pub const MH_DYLIB = 0x6; /// dynamically bound shared library
386pub const MH_DYLINKER = 0x7; /// dynamic link editor
387pub const MH_BUNDLE = 0x8; /// dynamically bound bundle file
388pub const MH_DYLIB_STUB = 0x9; /// shared library stub for static linking only, no section contents
389pub const MH_DSYM = 0xa; /// companion file with only debug sections
390pub const MH_KEXT_BUNDLE = 0xb; /// x86_64 kexts
391
392// Constants for the flags field of the mach_header
393
394pub const MH_NOUNDEFS = 0x1; /// the object file has no undefined references
395pub const MH_INCRLINK = 0x2; /// the object file is the output of an incremental link against a base file and can't be link edited again
396pub const MH_DYLDLINK = 0x4; /// the object file is input for the dynamic linker and can't be staticly link edited again
397pub const MH_BINDATLOAD = 0x8; /// the object file's undefined references are bound by the dynamic linker when loaded.
398pub const MH_PREBOUND = 0x10; /// the file has its dynamic undefined references prebound.
399pub const MH_SPLIT_SEGS = 0x20; /// the file has its read-only and read-write segments split
400pub const MH_LAZY_INIT = 0x40; /// the shared library init routine is to be run lazily via catching memory faults to its writeable segments (obsolete)
401pub const MH_TWOLEVEL = 0x80; /// the image is using two-level name space bindings
402pub const MH_FORCE_FLAT = 0x100; /// the executable is forcing all images to use flat name space bindings
403pub const MH_NOMULTIDEFS = 0x200; /// this umbrella guarantees no multiple defintions of symbols in its sub-images so the two-level namespace hints can always be used.
404pub const MH_NOFIXPREBINDING = 0x400; /// do not have dyld notify the prebinding agent about this executable
405pub const MH_PREBINDABLE = 0x800; /// the binary is not prebound but can have its prebinding redone. only used when MH_PREBOUND is not set.
406pub const MH_ALLMODSBOUND = 0x1000; /// indicates that this binary binds to all two-level namespace modules of its dependent libraries. only used when MH_PREBINDABLE and MH_TWOLEVEL are both set.
407pub const MH_SUBSECTIONS_VIA_SYMBOLS = 0x2000;/// safe to divide up the sections into sub-sections via symbols for dead code stripping
408pub const MH_CANONICAL = 0x4000; /// the binary has been canonicalized via the unprebind operation
409pub const MH_WEAK_DEFINES = 0x8000; /// the final linked image contains external weak symbols
410pub const MH_BINDS_TO_WEAK = 0x10000; /// the final linked image uses weak symbols
411
412pub const MH_ALLOW_STACK_EXECUTION = 0x20000;/// When this bit is set, all stacks in the task will be given stack execution privilege. Only used in MH_EXECUTE filetypes.
413pub const MH_ROOT_SAFE = 0x40000; /// When this bit is set, the binary declares it is safe for use in processes with uid zero
414
415pub const MH_SETUID_SAFE = 0x80000; /// When this bit is set, the binary declares it is safe for use in processes when issetugid() is true
416
417pub const MH_NO_REEXPORTED_DYLIBS = 0x100000; /// When this bit is set on a dylib, the static linker does not need to examine dependent dylibs to see if any are re-exported
418pub const MH_PIE = 0x200000; /// When this bit is set, the OS will load the main executable at a random address. Only used in MH_EXECUTE filetypes.
419pub const MH_DEAD_STRIPPABLE_DYLIB = 0x400000; /// Only for use on dylibs. When linking against a dylib that has this bit set, the static linker will automatically not create a LC_LOAD_DYLIB load command to the dylib if no symbols are being referenced from the dylib.
420pub const MH_HAS_TLV_DESCRIPTORS = 0x800000; /// Contains a section of type S_THREAD_LOCAL_VARIABLES
421
422pub const MH_NO_HEAP_EXECUTION = 0x1000000; /// When this bit is set, the OS will run the main executable with a non-executable heap even on platforms (e.g. i386) that don't require it. Only used in MH_EXECUTE filetypes.
423
424pub const MH_APP_EXTENSION_SAFE = 0x02000000; /// The code was linked for use in an application extension.
425
426pub const MH_NLIST_OUTOFSYNC_WITH_DYLDINFO = 0x04000000; /// The external symbols listed in the nlist symbol table do not include all the symbols listed in the dyld info.
427
428
429/// The flags field of a section structure is separated into two parts a section
430/// type and section attributes. The section types are mutually exclusive (it
431/// can only have one type) but the section attributes are not (it may have more
432/// than one attribute).
433/// 256 section types
434pub const SECTION_TYPE = 0x000000ff;
435pub const SECTION_ATTRIBUTES = 0xffffff00; /// 24 section attributes
436
437pub const S_REGULAR = 0x0; /// regular section
438pub const S_ZEROFILL = 0x1; /// zero fill on demand section
439pub const S_CSTRING_LITERALS = 0x2; /// section with only literal C string
440pub const S_4BYTE_LITERALS = 0x3; /// section with only 4 byte literals
441pub const S_8BYTE_LITERALS = 0x4; /// section with only 8 byte literals
442pub const S_LITERAL_POINTERS = 0x5; /// section with only pointers to
443
444
445pub const N_STAB = 0xe0; /// if any of these bits set, a symbolic debugging entry
446pub const N_PEXT = 0x10; /// private external symbol bit
447pub const N_TYPE = 0x0e; /// mask for the type bits
448pub const N_EXT = 0x01; /// external symbol bit, set for external symbols
449
450
451pub const N_GSYM = 0x20; /// global symbol: name,,NO_SECT,type,0
452pub const N_FNAME = 0x22; /// procedure name (f77 kludge): name,,NO_SECT,0,0
453pub const N_FUN = 0x24; /// procedure: name,,n_sect,linenumber,address
454pub const N_STSYM = 0x26; /// static symbol: name,,n_sect,type,address
455pub const N_LCSYM = 0x28; /// .lcomm symbol: name,,n_sect,type,address
456pub const N_BNSYM = 0x2e; /// begin nsect sym: 0,,n_sect,0,address
457pub const N_AST = 0x32; /// AST file path: name,,NO_SECT,0,0
458pub const N_OPT = 0x3c; /// emitted with gcc2_compiled and in gcc source
459pub const N_RSYM = 0x40; /// register sym: name,,NO_SECT,type,register
460pub const N_SLINE = 0x44; /// src line: 0,,n_sect,linenumber,address
461pub const N_ENSYM = 0x4e; /// end nsect sym: 0,,n_sect,0,address
462pub const N_SSYM = 0x60; /// structure elt: name,,NO_SECT,type,struct_offset
463pub const N_SO = 0x64; /// source file name: name,,n_sect,0,address
464pub const N_OSO = 0x66; /// object file name: name,,0,0,st_mtime
465pub const N_LSYM = 0x80; /// local sym: name,,NO_SECT,type,offset
466pub const N_BINCL = 0x82; /// include file beginning: name,,NO_SECT,0,sum
467pub const N_SOL = 0x84; /// #included file name: name,,n_sect,0,address
468pub const N_PARAMS = 0x86; /// compiler parameters: name,,NO_SECT,0,0
469pub const N_VERSION = 0x88; /// compiler version: name,,NO_SECT,0,0
470pub const N_OLEVEL = 0x8A; /// compiler -O level: name,,NO_SECT,0,0
471pub const N_PSYM = 0xa0; /// parameter: name,,NO_SECT,type,offset
472pub const N_EINCL = 0xa2; /// include file end: name,,NO_SECT,0,0
473pub const N_ENTRY = 0xa4; /// alternate entry: name,,n_sect,linenumber,address
474pub const N_LBRAC = 0xc0; /// left bracket: 0,,NO_SECT,nesting level,address
475pub const N_EXCL = 0xc2; /// deleted include file: name,,NO_SECT,0,sum
476pub const N_RBRAC = 0xe0; /// right bracket: 0,,NO_SECT,nesting level,address
477pub const N_BCOMM = 0xe2; /// begin common: name,,NO_SECT,0,0
478pub const N_ECOMM = 0xe4; /// end common: name,,n_sect,0,0
479pub const N_ECOML = 0xe8; /// end common (local name): 0,,n_sect,0,address
480pub const N_LENG = 0xfe; /// second stab entry with length information
481
482/// If a segment contains any sections marked with S_ATTR_DEBUG then all
483/// sections in that segment must have this attribute. No section other than
484/// a section marked with this attribute may reference the contents of this
485/// section. A section with this attribute may contain no symbols and must have
486/// a section type S_REGULAR. The static linker will not copy section contents
487/// from sections with this attribute into its output file. These sections
488/// generally contain DWARF debugging info.
489pub const S_ATTR_DEBUG = 0x02000000; /// a debug section
490
491pub const cpu_type_t = integer_t;
492pub const cpu_subtype_t = integer_t;
493pub const integer_t = c_int;
494pub const vm_prot_t = c_int;
495
496154// sys/types.h on macos uses #pragma pack(4) so these checks are
497155// to make sure the struct is laid out the same. These values were
498156// produced from C code using the offsetof macro.
std/c/linux.zig+3
......@@ -8,3 +8,6 @@ pub const pthread_attr_t = extern struct {
88 __size: [56]u8,
99 __align: c_long,
1010};
11
12/// See std.elf for constants for this
13pub extern fn getauxval(__type: c_ulong) c_ulong;
std/debug/index.zig+85-58
......@@ -4,6 +4,7 @@ const mem = std.mem;
44const io = std.io;
55const os = std.os;
66const elf = std.elf;
7const macho = std.macho;
78const DW = std.dwarf;
89const ArrayList = std.ArrayList;
910const builtin = @import("builtin");
......@@ -369,33 +370,7 @@ pub const OpenSelfDebugInfoError = error{
369370
370371pub fn openSelfDebugInfo(allocator: *mem.Allocator) !DebugInfo {
371372 switch (builtin.os) {
372 builtin.Os.linux => {
373 const st = try allocator.create(DebugInfo{
374 .self_exe_file = undefined,
375 .elf = undefined,
376 .debug_info = undefined,
377 .debug_abbrev = undefined,
378 .debug_str = undefined,
379 .debug_line = undefined,
380 .debug_ranges = null,
381 .abbrev_table_list = ArrayList(AbbrevTableHeader).init(allocator),
382 .compile_unit_list = ArrayList(CompileUnit).init(allocator),
383 });
384 errdefer allocator.destroy(st);
385 st.self_exe_file = try os.openSelfExe();
386 errdefer st.self_exe_file.close();
387
388 try st.elf.openFile(allocator, &st.self_exe_file);
389 errdefer st.elf.close();
390
391 st.debug_info = (try st.elf.findSection(".debug_info")) orelse return error.MissingDebugInfo;
392 st.debug_abbrev = (try st.elf.findSection(".debug_abbrev")) orelse return error.MissingDebugInfo;
393 st.debug_str = (try st.elf.findSection(".debug_str")) orelse return error.MissingDebugInfo;
394 st.debug_line = (try st.elf.findSection(".debug_line")) orelse return error.MissingDebugInfo;
395 st.debug_ranges = (try st.elf.findSection(".debug_ranges"));
396 try scanAllCompileUnits(st);
397 return st;
398 },
373 builtin.Os.linux => return openSelfDebugInfoLinux(allocator),
399374 builtin.Os.macosx, builtin.Os.ios => return openSelfDebugInfoMacOs(allocator),
400375 builtin.Os.windows => {
401376 // TODO: https://github.com/ziglang/zig/issues/721
......@@ -405,40 +380,91 @@ pub fn openSelfDebugInfo(allocator: *mem.Allocator) !DebugInfo {
405380 }
406381}
407382
383fn openSelfDebugInfoLinux(allocator: *mem.Allocator) !DebugInfo {
384 var di = DebugInfo{
385 .self_exe_file = undefined,
386 .elf = undefined,
387 .debug_info = undefined,
388 .debug_abbrev = undefined,
389 .debug_str = undefined,
390 .debug_line = undefined,
391 .debug_ranges = null,
392 .abbrev_table_list = ArrayList(AbbrevTableHeader).init(allocator),
393 .compile_unit_list = ArrayList(CompileUnit).init(allocator),
394 };
395 di.self_exe_file = try os.openSelfExe();
396 errdefer di.self_exe_file.close();
397
398 try di.elf.openFile(allocator, &di.self_exe_file);
399 errdefer di.elf.close();
400
401 di.debug_info = (try di.elf.findSection(".debug_info")) orelse return error.MissingDebugInfo;
402 di.debug_abbrev = (try di.elf.findSection(".debug_abbrev")) orelse return error.MissingDebugInfo;
403 di.debug_str = (try di.elf.findSection(".debug_str")) orelse return error.MissingDebugInfo;
404 di.debug_line = (try di.elf.findSection(".debug_line")) orelse return error.MissingDebugInfo;
405 di.debug_ranges = (try di.elf.findSection(".debug_ranges"));
406 try scanAllCompileUnits(&di);
407 return di;
408}
409
410pub fn findElfSection(elf: *Elf, name: []const u8) ?*elf.Shdr {
411 var file_stream = io.FileInStream.init(elf.in_file);
412 const in = &file_stream.stream;
413
414 section_loop: for (elf.section_headers) |*elf_section| {
415 if (elf_section.sh_type == SHT_NULL) continue;
416
417 const name_offset = elf.string_section.offset + elf_section.name;
418 try elf.in_file.seekTo(name_offset);
419
420 for (name) |expected_c| {
421 const target_c = try in.readByte();
422 if (target_c == 0 or expected_c != target_c) continue :section_loop;
423 }
424
425 {
426 const null_byte = try in.readByte();
427 if (null_byte == 0) return elf_section;
428 }
429 }
430
431 return null;
432}
433
408434fn openSelfDebugInfoMacOs(allocator: *mem.Allocator) !DebugInfo {
409435 const hdr = &std.c._mh_execute_header;
410 assert(hdr.magic == std.c.MH_MAGIC_64);
436 assert(hdr.magic == std.macho.MH_MAGIC_64);
411437
412438 const hdr_base = @ptrCast([*]u8, hdr);
413 var ptr = hdr_base + @sizeOf(std.c.mach_header_64);
439 var ptr = hdr_base + @sizeOf(macho.mach_header_64);
414440 var ncmd: u32 = hdr.ncmds;
415441 const symtab = while (ncmd != 0) : (ncmd -= 1) {
416 const lc = @ptrCast(*std.c.load_command, ptr);
442 const lc = @ptrCast(*std.macho.load_command, ptr);
417443 switch (lc.cmd) {
418 std.c.LC_SYMTAB => break @ptrCast(*std.c.symtab_command, ptr),
444 std.macho.LC_SYMTAB => break @ptrCast(*std.macho.symtab_command, ptr),
419445 else => {},
420446 }
421447 ptr += lc.cmdsize; // TODO https://github.com/ziglang/zig/issues/1403
422448 } else {
423449 return error.MissingDebugInfo;
424450 };
425 const syms = @ptrCast([*]std.c.nlist_64, hdr_base + symtab.symoff)[0..symtab.nsyms];
451 const syms = @ptrCast([*]macho.nlist_64, hdr_base + symtab.symoff)[0..symtab.nsyms];
426452 const strings = @ptrCast([*]u8, hdr_base + symtab.stroff)[0..symtab.strsize];
427453
428454 const symbols_buf = try allocator.alloc(MachoSymbol, syms.len);
429455
430 var ofile: ?*std.c.nlist_64 = null;
456 var ofile: ?*macho.nlist_64 = null;
431457 var reloc: u64 = 0;
432458 var symbol_index: usize = 0;
433459 var last_len: u64 = 0;
434460 for (syms) |*sym| {
435 if (sym.n_type & std.c.N_STAB != 0) {
461 if (sym.n_type & std.macho.N_STAB != 0) {
436462 switch (sym.n_type) {
437 std.c.N_OSO => {
463 std.macho.N_OSO => {
438464 ofile = sym;
439465 reloc = 0;
440466 },
441 std.c.N_FUN => {
467 std.macho.N_FUN => {
442468 if (sym.n_sect == 0) {
443469 last_len = sym.n_value;
444470 } else {
......@@ -450,7 +476,7 @@ fn openSelfDebugInfoMacOs(allocator: *mem.Allocator) !DebugInfo {
450476 symbol_index += 1;
451477 }
452478 },
453 std.c.N_BNSYM => {
479 std.macho.N_BNSYM => {
454480 if (reloc == 0) {
455481 reloc = sym.n_value;
456482 }
......@@ -459,8 +485,8 @@ fn openSelfDebugInfoMacOs(allocator: *mem.Allocator) !DebugInfo {
459485 }
460486 }
461487 }
462 const sentinel = try allocator.createOne(std.c.nlist_64);
463 sentinel.* = std.c.nlist_64{
488 const sentinel = try allocator.createOne(macho.nlist_64);
489 sentinel.* = macho.nlist_64{
464490 .n_strx = 0,
465491 .n_type = 36,
466492 .n_sect = 0,
......@@ -515,8 +541,8 @@ fn printLineFromFile(out_stream: var, line_info: *const LineInfo) !void {
515541}
516542
517543const MachoSymbol = struct {
518 nlist: *std.c.nlist_64,
519 ofile: ?*std.c.nlist_64,
544 nlist: *macho.nlist_64,
545 ofile: ?*macho.nlist_64,
520546 reloc: u64,
521547
522548 /// Returns the address from the macho file
......@@ -530,9 +556,9 @@ const MachoSymbol = struct {
530556};
531557
532558const MachOFile = struct {
533 bytes: []align(@alignOf(std.c.mach_header_64)) const u8,
534 sect_debug_info: ?*const std.c.section_64,
535 sect_debug_line: ?*const std.c.section_64,
559 bytes: []align(@alignOf(macho.mach_header_64)) const u8,
560 sect_debug_info: ?*const macho.section_64,
561 sect_debug_line: ?*const macho.section_64,
536562};
537563
538564pub const DebugInfo = switch (builtin.os) {
......@@ -542,10 +568,10 @@ pub const DebugInfo = switch (builtin.os) {
542568 ofiles: OFileTable,
543569
544570 const OFileTable = std.HashMap(
545 *std.c.nlist_64,
571 *macho.nlist_64,
546572 MachOFile,
547 std.hash_map.getHashPtrAddrFn(*std.c.nlist_64),
548 std.hash_map.getTrivialEqlFn(*std.c.nlist_64),
573 std.hash_map.getHashPtrAddrFn(*macho.nlist_64),
574 std.hash_map.getTrivialEqlFn(*macho.nlist_64),
549575 );
550576
551577 pub fn allocator(self: DebugInfo) *mem.Allocator {
......@@ -563,7 +589,7 @@ pub const DebugInfo = switch (builtin.os) {
563589 abbrev_table_list: ArrayList(AbbrevTableHeader),
564590 compile_unit_list: ArrayList(CompileUnit),
565591
566 pub fn allocator(self: *const DebugInfo) *mem.Allocator {
592 pub fn allocator(self: DebugInfo) *mem.Allocator {
567593 return self.abbrev_table_list.allocator;
568594 }
569595
......@@ -983,30 +1009,31 @@ fn getLineNumberInfoMacOs(di: *DebugInfo, symbol: MachoSymbol, target_address: u
9831009 const ofile_path = mem.toSliceConst(u8, di.strings.ptr + ofile.n_strx);
9841010
9851011 gop.kv.value = MachOFile{
986 .bytes = try std.io.readFileAllocAligned(di.ofiles.allocator, ofile_path, @alignOf(std.c.mach_header_64)),
1012 .bytes = try std.io.readFileAllocAligned(di.ofiles.allocator, ofile_path, @alignOf(macho.mach_header_64)),
9871013 .sect_debug_info = null,
9881014 .sect_debug_line = null,
9891015 };
990 const hdr = @ptrCast(*const std.c.mach_header_64, gop.kv.value.bytes.ptr);
991 if (hdr.magic != std.c.MH_MAGIC_64) return error.InvalidDebugInfo;
1016 const hdr = @ptrCast(*const macho.mach_header_64, gop.kv.value.bytes.ptr);
1017 if (hdr.magic != std.macho.MH_MAGIC_64) return error.InvalidDebugInfo;
9921018
9931019 const hdr_base = @ptrCast([*]const u8, hdr);
994 var ptr = hdr_base + @sizeOf(std.c.mach_header_64);
1020 var ptr = hdr_base + @sizeOf(macho.mach_header_64);
9951021 var ncmd: u32 = hdr.ncmds;
9961022 const segcmd = while (ncmd != 0) : (ncmd -= 1) {
997 const lc = @ptrCast(*const std.c.load_command, ptr);
1023 const lc = @ptrCast(*const std.macho.load_command, ptr);
9981024 switch (lc.cmd) {
999 std.c.LC_SEGMENT_64 => break @ptrCast(*const std.c.segment_command_64, ptr),
1025 std.macho.LC_SEGMENT_64 => break @ptrCast(*const std.macho.segment_command_64, ptr),
10001026 else => {},
10011027 }
10021028 ptr += lc.cmdsize; // TODO https://github.com/ziglang/zig/issues/1403
10031029 } else {
10041030 return error.MissingDebugInfo;
10051031 };
1006 const sections = @alignCast(@alignOf(std.c.section_64), @ptrCast([*]const std.c.section_64, ptr + @sizeOf(std.c.segment_command_64)))[0..segcmd.nsects];
1032 const sections = @alignCast(@alignOf(macho.section_64), @ptrCast([*]const macho.section_64, ptr + @sizeOf(std.macho.segment_command_64)))[0..segcmd.nsects];
10071033 for (sections) |*sect| {
1008 if (sect.flags & std.c.SECTION_TYPE == std.c.S_REGULAR and
1009 (sect.flags & std.c.SECTION_ATTRIBUTES) & std.c.S_ATTR_DEBUG == std.c.S_ATTR_DEBUG) {
1034 if (sect.flags & macho.SECTION_TYPE == macho.S_REGULAR and
1035 (sect.flags & macho.SECTION_ATTRIBUTES) & macho.S_ATTR_DEBUG == macho.S_ATTR_DEBUG)
1036 {
10101037 const sect_name = mem.toSliceConst(u8, &sect.sectname);
10111038 if (mem.eql(u8, sect_name, "__debug_line")) {
10121039 gop.kv.value.sect_debug_line = sect;
......@@ -1052,7 +1079,7 @@ fn getLineNumberInfoMacOs(di: *DebugInfo, symbol: MachoSymbol, target_address: u
10521079
10531080 const opcode_base = readByteMem(&ptr);
10541081
1055 const standard_opcode_lengths = ptr[0..opcode_base - 1];
1082 const standard_opcode_lengths = ptr[0 .. opcode_base - 1];
10561083 ptr += opcode_base - 1;
10571084
10581085 var include_directories = ArrayList([]const u8).init(di.allocator());
std/elf.zig+5
......@@ -869,6 +869,11 @@ pub const Phdr = switch (@sizeOf(usize)) {
869869 8 => Elf64_Phdr,
870870 else => @compileError("expected pointer size of 32 or 64"),
871871};
872pub const Shdr = switch (@sizeOf(usize)) {
873 4 => Elf32_Shdr,
874 8 => Elf64_Shdr,
875 else => @compileError("expected pointer size of 32 or 64"),
876};
872877pub const Sym = switch (@sizeOf(usize)) {
873878 4 => Elf32_Sym,
874879 8 => Elf64_Sym,
std/macho.zig created+348
......@@ -0,0 +1,348 @@
1
2pub const mach_header = extern struct {
3 magic: u32,
4 cputype: cpu_type_t,
5 cpusubtype: cpu_subtype_t,
6 filetype: u32,
7 ncmds: u32,
8 sizeofcmds: u32,
9 flags: u32,
10};
11
12pub const mach_header_64 = extern struct {
13 magic: u32,
14 cputype: cpu_type_t,
15 cpusubtype: cpu_subtype_t,
16 filetype: u32,
17 ncmds: u32,
18 sizeofcmds: u32,
19 flags: u32,
20 reserved: u32,
21};
22
23pub const load_command = extern struct {
24 cmd: u32,
25 cmdsize: u32,
26};
27
28
29/// The symtab_command contains the offsets and sizes of the link-edit 4.3BSD
30/// "stab" style symbol table information as described in the header files
31/// <nlist.h> and <stab.h>.
32pub const symtab_command = extern struct {
33 cmd: u32, /// LC_SYMTAB
34 cmdsize: u32, /// sizeof(struct symtab_command)
35 symoff: u32, /// symbol table offset
36 nsyms: u32, /// number of symbol table entries
37 stroff: u32, /// string table offset
38 strsize: u32, /// string table size in bytes
39};
40
41/// The linkedit_data_command contains the offsets and sizes of a blob
42/// of data in the __LINKEDIT segment.
43const linkedit_data_command = extern struct {
44 cmd: u32,/// LC_CODE_SIGNATURE, LC_SEGMENT_SPLIT_INFO, LC_FUNCTION_STARTS, LC_DATA_IN_CODE, LC_DYLIB_CODE_SIGN_DRS or LC_LINKER_OPTIMIZATION_HINT.
45 cmdsize: u32, /// sizeof(struct linkedit_data_command)
46 dataoff: u32 , /// file offset of data in __LINKEDIT segment
47 datasize: u32 , /// file size of data in __LINKEDIT segment
48};
49
50/// The segment load command indicates that a part of this file is to be
51/// mapped into the task's address space. The size of this segment in memory,
52/// vmsize, maybe equal to or larger than the amount to map from this file,
53/// filesize. The file is mapped starting at fileoff to the beginning of
54/// the segment in memory, vmaddr. The rest of the memory of the segment,
55/// if any, is allocated zero fill on demand. The segment's maximum virtual
56/// memory protection and initial virtual memory protection are specified
57/// by the maxprot and initprot fields. If the segment has sections then the
58/// section structures directly follow the segment command and their size is
59/// reflected in cmdsize.
60pub const segment_command = extern struct {
61 cmd: u32,/// LC_SEGMENT
62 cmdsize: u32,/// includes sizeof section structs
63 segname: [16]u8,/// segment name
64 vmaddr: u32,/// memory address of this segment
65 vmsize: u32,/// memory size of this segment
66 fileoff: u32,/// file offset of this segment
67 filesize: u32,/// amount to map from the file
68 maxprot: vm_prot_t,/// maximum VM protection
69 initprot: vm_prot_t,/// initial VM protection
70 nsects: u32,/// number of sections in segment
71 flags: u32,
72};
73
74/// The 64-bit segment load command indicates that a part of this file is to be
75/// mapped into a 64-bit task's address space. If the 64-bit segment has
76/// sections then section_64 structures directly follow the 64-bit segment
77/// command and their size is reflected in cmdsize.
78pub const segment_command_64 = extern struct {
79 cmd: u32, /// LC_SEGMENT_64
80 cmdsize: u32, /// includes sizeof section_64 structs
81 segname: [16]u8, /// segment name
82 vmaddr: u64, /// memory address of this segment
83 vmsize: u64, /// memory size of this segment
84 fileoff: u64, /// file offset of this segment
85 filesize: u64, /// amount to map from the file
86 maxprot: vm_prot_t, /// maximum VM protection
87 initprot: vm_prot_t, /// initial VM protection
88 nsects: u32, /// number of sections in segment
89 flags: u32,
90};
91
92/// A segment is made up of zero or more sections. Non-MH_OBJECT files have
93/// all of their segments with the proper sections in each, and padded to the
94/// specified segment alignment when produced by the link editor. The first
95/// segment of a MH_EXECUTE and MH_FVMLIB format file contains the mach_header
96/// and load commands of the object file before its first section. The zero
97/// fill sections are always last in their segment (in all formats). This
98/// allows the zeroed segment padding to be mapped into memory where zero fill
99/// sections might be. The gigabyte zero fill sections, those with the section
100/// type S_GB_ZEROFILL, can only be in a segment with sections of this type.
101/// These segments are then placed after all other segments.
102///
103/// The MH_OBJECT format has all of its sections in one segment for
104/// compactness. There is no padding to a specified segment boundary and the
105/// mach_header and load commands are not part of the segment.
106///
107/// Sections with the same section name, sectname, going into the same segment,
108/// segname, are combined by the link editor. The resulting section is aligned
109/// to the maximum alignment of the combined sections and is the new section's
110/// alignment. The combined sections are aligned to their original alignment in
111/// the combined section. Any padded bytes to get the specified alignment are
112/// zeroed.
113///
114/// The format of the relocation entries referenced by the reloff and nreloc
115/// fields of the section structure for mach object files is described in the
116/// header file <reloc.h>.
117pub const @"section" = extern struct {
118 sectname: [16]u8, /// name of this section
119 segname: [16]u8, /// segment this section goes in
120 addr: u32, /// memory address of this section
121 size: u32, /// size in bytes of this section
122 offset: u32, /// file offset of this section
123 @"align": u32, /// section alignment (power of 2)
124 reloff: u32, /// file offset of relocation entries
125 nreloc: u32, /// number of relocation entries
126 flags: u32, /// flags (section type and attributes
127 reserved1: u32, /// reserved (for offset or index)
128 reserved2: u32, /// reserved (for count or sizeof)
129};
130
131pub const section_64 = extern struct {
132 sectname: [16]u8, /// name of this section
133 segname: [16]u8, /// segment this section goes in
134 addr: u64, /// memory address of this section
135 size: u64, /// size in bytes of this section
136 offset: u32, /// file offset of this section
137 @"align": u32, /// section alignment (power of 2)
138 reloff: u32, /// file offset of relocation entries
139 nreloc: u32, /// number of relocation entries
140 flags: u32, /// flags (section type and attributes
141 reserved1: u32, /// reserved (for offset or index)
142 reserved2: u32, /// reserved (for count or sizeof)
143 reserved3: u32, /// reserved
144};
145
146pub const nlist = extern struct {
147 n_strx: u32,
148 n_type: u8,
149 n_sect: u8,
150 n_desc: i16,
151 n_value: u32,
152};
153
154pub const nlist_64 = extern struct {
155 n_strx: u32,
156 n_type: u8,
157 n_sect: u8,
158 n_desc: u16,
159 n_value: u64,
160};
161
162/// After MacOS X 10.1 when a new load command is added that is required to be
163/// understood by the dynamic linker for the image to execute properly the
164/// LC_REQ_DYLD bit will be or'ed into the load command constant. If the dynamic
165/// linker sees such a load command it it does not understand will issue a
166/// "unknown load command required for execution" error and refuse to use the
167/// image. Other load commands without this bit that are not understood will
168/// simply be ignored.
169pub const LC_REQ_DYLD = 0x80000000;
170
171pub const LC_SEGMENT = 0x1; /// segment of this file to be mapped
172pub const LC_SYMTAB = 0x2; /// link-edit stab symbol table info
173pub const LC_SYMSEG = 0x3; /// link-edit gdb symbol table info (obsolete)
174pub const LC_THREAD = 0x4; /// thread
175pub const LC_UNIXTHREAD = 0x5; /// unix thread (includes a stack)
176pub const LC_LOADFVMLIB = 0x6; /// load a specified fixed VM shared library
177pub const LC_IDFVMLIB = 0x7; /// fixed VM shared library identification
178pub const LC_IDENT = 0x8; /// object identification info (obsolete)
179pub const LC_FVMFILE = 0x9; /// fixed VM file inclusion (internal use)
180pub const LC_PREPAGE = 0xa; /// prepage command (internal use)
181pub const LC_DYSYMTAB = 0xb; /// dynamic link-edit symbol table info
182pub const LC_LOAD_DYLIB = 0xc; /// load a dynamically linked shared library
183pub const LC_ID_DYLIB = 0xd; /// dynamically linked shared lib ident
184pub const LC_LOAD_DYLINKER = 0xe; /// load a dynamic linker
185pub const LC_ID_DYLINKER = 0xf; /// dynamic linker identification
186pub const LC_PREBOUND_DYLIB = 0x10; /// modules prebound for a dynamically
187pub const LC_ROUTINES = 0x11; /// image routines
188pub const LC_SUB_FRAMEWORK = 0x12; /// sub framework
189pub const LC_SUB_UMBRELLA = 0x13; /// sub umbrella
190pub const LC_SUB_CLIENT = 0x14; /// sub client
191pub const LC_SUB_LIBRARY = 0x15; /// sub library
192pub const LC_TWOLEVEL_HINTS = 0x16; /// two-level namespace lookup hints
193pub const LC_PREBIND_CKSUM = 0x17; /// prebind checksum
194
195/// load a dynamically linked shared library that is allowed to be missing
196/// (all symbols are weak imported).
197pub const LC_LOAD_WEAK_DYLIB = (0x18 | LC_REQ_DYLD);
198
199pub const LC_SEGMENT_64 = 0x19; /// 64-bit segment of this file to be mapped
200pub const LC_ROUTINES_64 = 0x1a; /// 64-bit image routines
201pub const LC_UUID = 0x1b; /// the uuid
202pub const LC_RPATH = (0x1c | LC_REQ_DYLD); /// runpath additions
203pub const LC_CODE_SIGNATURE = 0x1d; /// local of code signature
204pub const LC_SEGMENT_SPLIT_INFO = 0x1e; /// local of info to split segments
205pub const LC_REEXPORT_DYLIB = (0x1f | LC_REQ_DYLD); /// load and re-export dylib
206pub const LC_LAZY_LOAD_DYLIB = 0x20; /// delay load of dylib until first use
207pub const LC_ENCRYPTION_INFO = 0x21; /// encrypted segment information
208pub const LC_DYLD_INFO = 0x22; /// compressed dyld information
209pub const LC_DYLD_INFO_ONLY = (0x22|LC_REQ_DYLD); /// compressed dyld information only
210pub const LC_LOAD_UPWARD_DYLIB = (0x23 | LC_REQ_DYLD); /// load upward dylib
211pub const LC_VERSION_MIN_MACOSX = 0x24; /// build for MacOSX min OS version
212pub const LC_VERSION_MIN_IPHONEOS = 0x25; /// build for iPhoneOS min OS version
213pub const LC_FUNCTION_STARTS = 0x26; /// compressed table of function start addresses
214pub const LC_DYLD_ENVIRONMENT = 0x27; /// string for dyld to treat like environment variable
215pub const LC_MAIN = (0x28|LC_REQ_DYLD); /// replacement for LC_UNIXTHREAD
216pub const LC_DATA_IN_CODE = 0x29; /// table of non-instructions in __text
217pub const LC_SOURCE_VERSION = 0x2A; /// source version used to build binary
218pub const LC_DYLIB_CODE_SIGN_DRS = 0x2B; /// Code signing DRs copied from linked dylibs
219pub const LC_ENCRYPTION_INFO_64 = 0x2C; /// 64-bit encrypted segment information
220pub const LC_LINKER_OPTION = 0x2D; /// linker options in MH_OBJECT files
221pub const LC_LINKER_OPTIMIZATION_HINT = 0x2E; /// optimization hints in MH_OBJECT files
222pub const LC_VERSION_MIN_TVOS = 0x2F; /// build for AppleTV min OS version
223pub const LC_VERSION_MIN_WATCHOS = 0x30; /// build for Watch min OS version
224pub const LC_NOTE = 0x31; /// arbitrary data included within a Mach-O file
225pub const LC_BUILD_VERSION = 0x32; /// build for platform min OS version
226
227pub const MH_MAGIC = 0xfeedface; /// the mach magic number
228pub const MH_CIGAM = 0xcefaedfe; /// NXSwapInt(MH_MAGIC)
229
230pub const MH_MAGIC_64 = 0xfeedfacf; /// the 64-bit mach magic number
231pub const MH_CIGAM_64 = 0xcffaedfe; /// NXSwapInt(MH_MAGIC_64)
232
233pub const MH_OBJECT = 0x1; /// relocatable object file
234pub const MH_EXECUTE = 0x2; /// demand paged executable file
235pub const MH_FVMLIB = 0x3; /// fixed VM shared library file
236pub const MH_CORE = 0x4; /// core file
237pub const MH_PRELOAD = 0x5; /// preloaded executable file
238pub const MH_DYLIB = 0x6; /// dynamically bound shared library
239pub const MH_DYLINKER = 0x7; /// dynamic link editor
240pub const MH_BUNDLE = 0x8; /// dynamically bound bundle file
241pub const MH_DYLIB_STUB = 0x9; /// shared library stub for static linking only, no section contents
242pub const MH_DSYM = 0xa; /// companion file with only debug sections
243pub const MH_KEXT_BUNDLE = 0xb; /// x86_64 kexts
244
245// Constants for the flags field of the mach_header
246
247pub const MH_NOUNDEFS = 0x1; /// the object file has no undefined references
248pub const MH_INCRLINK = 0x2; /// the object file is the output of an incremental link against a base file and can't be link edited again
249pub const MH_DYLDLINK = 0x4; /// the object file is input for the dynamic linker and can't be staticly link edited again
250pub const MH_BINDATLOAD = 0x8; /// the object file's undefined references are bound by the dynamic linker when loaded.
251pub const MH_PREBOUND = 0x10; /// the file has its dynamic undefined references prebound.
252pub const MH_SPLIT_SEGS = 0x20; /// the file has its read-only and read-write segments split
253pub const MH_LAZY_INIT = 0x40; /// the shared library init routine is to be run lazily via catching memory faults to its writeable segments (obsolete)
254pub const MH_TWOLEVEL = 0x80; /// the image is using two-level name space bindings
255pub const MH_FORCE_FLAT = 0x100; /// the executable is forcing all images to use flat name space bindings
256pub const MH_NOMULTIDEFS = 0x200; /// this umbrella guarantees no multiple defintions of symbols in its sub-images so the two-level namespace hints can always be used.
257pub const MH_NOFIXPREBINDING = 0x400; /// do not have dyld notify the prebinding agent about this executable
258pub const MH_PREBINDABLE = 0x800; /// the binary is not prebound but can have its prebinding redone. only used when MH_PREBOUND is not set.
259pub const MH_ALLMODSBOUND = 0x1000; /// indicates that this binary binds to all two-level namespace modules of its dependent libraries. only used when MH_PREBINDABLE and MH_TWOLEVEL are both set.
260pub const MH_SUBSECTIONS_VIA_SYMBOLS = 0x2000;/// safe to divide up the sections into sub-sections via symbols for dead code stripping
261pub const MH_CANONICAL = 0x4000; /// the binary has been canonicalized via the unprebind operation
262pub const MH_WEAK_DEFINES = 0x8000; /// the final linked image contains external weak symbols
263pub const MH_BINDS_TO_WEAK = 0x10000; /// the final linked image uses weak symbols
264
265pub const MH_ALLOW_STACK_EXECUTION = 0x20000;/// When this bit is set, all stacks in the task will be given stack execution privilege. Only used in MH_EXECUTE filetypes.
266pub const MH_ROOT_SAFE = 0x40000; /// When this bit is set, the binary declares it is safe for use in processes with uid zero
267
268pub const MH_SETUID_SAFE = 0x80000; /// When this bit is set, the binary declares it is safe for use in processes when issetugid() is true
269
270pub const MH_NO_REEXPORTED_DYLIBS = 0x100000; /// When this bit is set on a dylib, the static linker does not need to examine dependent dylibs to see if any are re-exported
271pub const MH_PIE = 0x200000; /// When this bit is set, the OS will load the main executable at a random address. Only used in MH_EXECUTE filetypes.
272pub const MH_DEAD_STRIPPABLE_DYLIB = 0x400000; /// Only for use on dylibs. When linking against a dylib that has this bit set, the static linker will automatically not create a LC_LOAD_DYLIB load command to the dylib if no symbols are being referenced from the dylib.
273pub const MH_HAS_TLV_DESCRIPTORS = 0x800000; /// Contains a section of type S_THREAD_LOCAL_VARIABLES
274
275pub const MH_NO_HEAP_EXECUTION = 0x1000000; /// When this bit is set, the OS will run the main executable with a non-executable heap even on platforms (e.g. i386) that don't require it. Only used in MH_EXECUTE filetypes.
276
277pub const MH_APP_EXTENSION_SAFE = 0x02000000; /// The code was linked for use in an application extension.
278
279pub const MH_NLIST_OUTOFSYNC_WITH_DYLDINFO = 0x04000000; /// The external symbols listed in the nlist symbol table do not include all the symbols listed in the dyld info.
280
281
282/// The flags field of a section structure is separated into two parts a section
283/// type and section attributes. The section types are mutually exclusive (it
284/// can only have one type) but the section attributes are not (it may have more
285/// than one attribute).
286/// 256 section types
287pub const SECTION_TYPE = 0x000000ff;
288pub const SECTION_ATTRIBUTES = 0xffffff00; /// 24 section attributes
289
290pub const S_REGULAR = 0x0; /// regular section
291pub const S_ZEROFILL = 0x1; /// zero fill on demand section
292pub const S_CSTRING_LITERALS = 0x2; /// section with only literal C string
293pub const S_4BYTE_LITERALS = 0x3; /// section with only 4 byte literals
294pub const S_8BYTE_LITERALS = 0x4; /// section with only 8 byte literals
295pub const S_LITERAL_POINTERS = 0x5; /// section with only pointers to
296
297
298pub const N_STAB = 0xe0; /// if any of these bits set, a symbolic debugging entry
299pub const N_PEXT = 0x10; /// private external symbol bit
300pub const N_TYPE = 0x0e; /// mask for the type bits
301pub const N_EXT = 0x01; /// external symbol bit, set for external symbols
302
303
304pub const N_GSYM = 0x20; /// global symbol: name,,NO_SECT,type,0
305pub const N_FNAME = 0x22; /// procedure name (f77 kludge): name,,NO_SECT,0,0
306pub const N_FUN = 0x24; /// procedure: name,,n_sect,linenumber,address
307pub const N_STSYM = 0x26; /// static symbol: name,,n_sect,type,address
308pub const N_LCSYM = 0x28; /// .lcomm symbol: name,,n_sect,type,address
309pub const N_BNSYM = 0x2e; /// begin nsect sym: 0,,n_sect,0,address
310pub const N_AST = 0x32; /// AST file path: name,,NO_SECT,0,0
311pub const N_OPT = 0x3c; /// emitted with gcc2_compiled and in gcc source
312pub const N_RSYM = 0x40; /// register sym: name,,NO_SECT,type,register
313pub const N_SLINE = 0x44; /// src line: 0,,n_sect,linenumber,address
314pub const N_ENSYM = 0x4e; /// end nsect sym: 0,,n_sect,0,address
315pub const N_SSYM = 0x60; /// structure elt: name,,NO_SECT,type,struct_offset
316pub const N_SO = 0x64; /// source file name: name,,n_sect,0,address
317pub const N_OSO = 0x66; /// object file name: name,,0,0,st_mtime
318pub const N_LSYM = 0x80; /// local sym: name,,NO_SECT,type,offset
319pub const N_BINCL = 0x82; /// include file beginning: name,,NO_SECT,0,sum
320pub const N_SOL = 0x84; /// #included file name: name,,n_sect,0,address
321pub const N_PARAMS = 0x86; /// compiler parameters: name,,NO_SECT,0,0
322pub const N_VERSION = 0x88; /// compiler version: name,,NO_SECT,0,0
323pub const N_OLEVEL = 0x8A; /// compiler -O level: name,,NO_SECT,0,0
324pub const N_PSYM = 0xa0; /// parameter: name,,NO_SECT,type,offset
325pub const N_EINCL = 0xa2; /// include file end: name,,NO_SECT,0,0
326pub const N_ENTRY = 0xa4; /// alternate entry: name,,n_sect,linenumber,address
327pub const N_LBRAC = 0xc0; /// left bracket: 0,,NO_SECT,nesting level,address
328pub const N_EXCL = 0xc2; /// deleted include file: name,,NO_SECT,0,sum
329pub const N_RBRAC = 0xe0; /// right bracket: 0,,NO_SECT,nesting level,address
330pub const N_BCOMM = 0xe2; /// begin common: name,,NO_SECT,0,0
331pub const N_ECOMM = 0xe4; /// end common: name,,n_sect,0,0
332pub const N_ECOML = 0xe8; /// end common (local name): 0,,n_sect,0,address
333pub const N_LENG = 0xfe; /// second stab entry with length information
334
335/// If a segment contains any sections marked with S_ATTR_DEBUG then all
336/// sections in that segment must have this attribute. No section other than
337/// a section marked with this attribute may reference the contents of this
338/// section. A section with this attribute may contain no symbols and must have
339/// a section type S_REGULAR. The static linker will not copy section contents
340/// from sections with this attribute into its output file. These sections
341/// generally contain DWARF debugging info.
342pub const S_ATTR_DEBUG = 0x02000000; /// a debug section
343
344pub const cpu_type_t = integer_t;
345pub const cpu_subtype_t = integer_t;
346pub const integer_t = c_int;
347pub const vm_prot_t = c_int;
348
std/os/index.zig+29
......@@ -635,6 +635,35 @@ fn posixExecveErrnoToErr(err: usize) PosixExecveError {
635635pub var linux_aux_raw = []usize{0} ** 38;
636636pub var posix_environ_raw: [][*]u8 = undefined;
637637
638/// See std.elf for the constants.
639pub fn linuxGetAuxVal(index: usize) usize {
640 if (builtin.link_libc) {
641 return usize(std.c.getauxval(index));
642 } else {
643 return linux_aux_raw[index];
644 }
645}
646
647pub fn getBaseAddress() usize {
648 switch (builtin.os) {
649 builtin.Os.linux => {
650 const base = linuxGetAuxVal(std.elf.AT_BASE);
651 if (base != 0) {
652 return base;
653 }
654 const phdr = linuxGetAuxVal(std.elf.AT_PHDR);
655 const ElfHeader = switch (@sizeOf(usize)) {
656 4 => std.elf.Elf32_Ehdr,
657 8 => std.elf.Elf64_Ehdr,
658 else => @compileError("Unsupported architecture"),
659 };
660 return phdr - @sizeOf(ElfHeader);
661 },
662 builtin.Os.macosx => return @ptrToInt(&std.c._mh_execute_header),
663 else => @compileError("Unsupported OS"),
664 }
665}
666
638667/// Caller must free result when done.
639668/// TODO make this go through libc when we have it
640669pub fn getEnvMap(allocator: *Allocator) !BufMap {