var typepkg *Pkg // fake package for runtime type info (headers)
 
-var typelinkpkg *Pkg // fake package for runtime type info (data)
-
 var unsafepkg *Pkg // package unsafe
 
 var trackpkg *Pkg // fake package for field tracking
 
        itabpkg.Name = "go.itab"
        itabpkg.Prefix = "go.itab" // not go%2eitab
 
-       typelinkpkg = mkpkg("go.typelink")
-       typelinkpkg.Name = "go.typelink"
-       typelinkpkg.Prefix = "go.typelink" // not go%2etypelink
-
        itablinkpkg = mkpkg("go.itablink")
        itablinkpkg.Name = "go.itablink"
        itablinkpkg.Prefix = "go.itablink" // not go%2eitablink
 
        return off
 }
 
+func dsymptrOffLSym(s *obj.LSym, off int, x *obj.LSym, xoff int) int {
+       s.WriteOff(Ctxt, int64(off), x, int64(xoff))
+       off += 4
+       return off
+}
+
 func gdata(nam *Node, nr *Node, wid int) {
        if nam.Op != ONAME {
                Fatalf("gdata nam op %v", opnames[nam.Op])
 
        return Pkglookup(Tconv(t, FmtLeft)+"."+f.Sym.Name, trackpkg)
 }
 
-func typelinksym(t *Type) *Sym {
+func typelinkLSym(t *Type) *obj.LSym {
        // %-uT is what the generated Type's string field says.
        // It uses (ambiguous) package names instead of import paths.
        // %-T is the complete, unambiguous type name.
        // ensure the types appear sorted by their string field. The
        // names are a little long but they are discarded by the linker
        // and do not end up in the symbol table of the final binary.
-       p := Tconv(t, FmtLeft|FmtUnsigned) + "\t" + Tconv(t, FmtLeft)
-
-       s := Pkglookup(p, typelinkpkg)
-
-       //print("typelinksym: %s -> %+S\n", p, s);
-
-       return s
+       name := "go.typelink." + Tconv(t, FmtLeft|FmtUnsigned) + "\t" + Tconv(t, FmtLeft)
+       return obj.Linklookup(Ctxt, name, 0)
 }
 
 func typesymprefix(prefix string, t *Type) *Sym {
        if t.Sym == nil {
                switch t.Etype {
                case TPTR32, TPTR64, TARRAY, TCHAN, TFUNC, TMAP, TSTRUCT:
-                       slink := typelinksym(t)
-                       dsymptr(slink, 0, s, 0)
-                       ggloblsym(slink, int32(Widthptr), int16(dupok|obj.RODATA))
+                       slink := typelinkLSym(t)
+                       dsymptrOffLSym(slink, 0, Linksym(s), 0)
+                       ggloblLSym(slink, 4, int16(dupok|obj.RODATA))
                }
        }
 
 
 // rsym and roff specify the relocation for the address.
 func (s *LSym) WriteAddr(ctxt *Link, off int64, siz int, rsym *LSym, roff int64) {
        if siz != ctxt.Arch.PtrSize {
-               ctxt.Diag("WriteAddr: bad address size: %d", siz)
+               ctxt.Diag("WriteAddr: bad address size %d in %s", siz, s.Name)
        }
        s.prepwrite(ctxt, off, siz)
        r := Addrel(s)
        r.Off = int32(off)
+       if int64(r.Off) != off {
+               ctxt.Diag("WriteAddr: off overflow %d in %s", off, s.Name)
+       }
        r.Siz = uint8(siz)
        r.Sym = rsym
        r.Type = R_ADDR
        r.Add = roff
 }
 
+// WriteOff writes a 4 byte offset to rsym+roff into s at offset off.
+// After linking the 4 bytes stored at s+off will be
+// rsym+roff-(start of section that s is in).
+func (s *LSym) WriteOff(ctxt *Link, off int64, rsym *LSym, roff int64) {
+       s.prepwrite(ctxt, off, 4)
+       r := Addrel(s)
+       r.Off = int32(off)
+       if int64(r.Off) != off {
+               ctxt.Diag("WriteOff: off overflow %d in %s", off, s.Name)
+       }
+       r.Siz = 4
+       r.Sym = rsym
+       r.Type = R_ADDROFF
+       r.Add = roff
+}
+
 // WriteString writes a string of size siz into s at offset off.
 func (s *LSym) WriteString(ctxt *Link, off int64, siz int, str string) {
        if siz < len(str) {
 
        // R_ADDRMIPS (only used on mips64) resolves to a 32-bit external address,
        // by loading the address into a register with two instructions (lui, ori).
        R_ADDRMIPS
+       // R_ADDROFF resolves to an offset from the beginning of the section holding
+       // the data being relocated to the referenced symbol.
+       R_ADDROFF
        R_SIZE
        R_CALL
        R_CALLARM
 
                        }
                        o = Symaddr(r.Sym) + r.Add - int64(r.Sym.Sect.Vaddr)
 
+               case obj.R_ADDROFF:
+                       o = Symaddr(r.Sym) - int64(r.Sym.Sect.Vaddr) + r.Add
+
                        // r->sym can be null when CALL $(constant) is transformed from absolute PC to relative PC call.
                case obj.R_CALL, obj.R_GOTPCREL, obj.R_PCREL:
                        if Linkmode == LinkExternal && r.Sym != nil && r.Sym.Type != obj.SCONST && (r.Sym.Sect != Ctxt.Cursym.Sect || r.Type == obj.R_GOTPCREL) {
        sect.Vaddr = 0
        Linklookup(Ctxt, "runtime.rodata", 0).Sect = sect
        Linklookup(Ctxt, "runtime.erodata", 0).Sect = sect
+       if !UseRelro() {
+               Linklookup(Ctxt, "runtime.types", 0).Sect = sect
+               Linklookup(Ctxt, "runtime.etypes", 0).Sect = sect
+       }
        for ; s != nil && s.Type < obj.STYPERELRO; s = s.Next {
                datsize = aligndatsize(datsize, s)
                s.Sect = sect
                sect.Align = maxalign(s, obj.STYPELINK-1)
                datsize = Rnd(datsize, int64(sect.Align))
                sect.Vaddr = 0
+               Linklookup(Ctxt, "runtime.types", 0).Sect = sect
+               Linklookup(Ctxt, "runtime.etypes", 0).Sect = sect
                for ; s != nil && s.Type < obj.STYPELINK; s = s.Next {
                        datsize = aligndatsize(datsize, s)
                        if s.Outer != nil && s.Outer.Sect != nil && s.Outer.Sect != sect {
        } else {
                rodata = text.Next
        }
+       var relrodata *Section
        typelink := rodata.Next
        if UseRelro() {
                // There is another section (.data.rel.ro) when building a shared
                // object on elf systems.
+               relrodata = typelink
                typelink = typelink.Next
        }
        itablink := typelink.Next
                s.Value = int64(sectSym.Sect.Vaddr + 16)
        }
 
+       types := relrodata
+       if types == nil {
+               types = rodata
+       }
+
        xdefine("runtime.text", obj.STEXT, int64(text.Vaddr))
        xdefine("runtime.etext", obj.STEXT, int64(text.Vaddr+text.Length))
        if HEADTYPE == obj.Hwindows {
        }
        xdefine("runtime.rodata", obj.SRODATA, int64(rodata.Vaddr))
        xdefine("runtime.erodata", obj.SRODATA, int64(rodata.Vaddr+rodata.Length))
+       xdefine("runtime.types", obj.SRODATA, int64(types.Vaddr))
+       xdefine("runtime.etypes", obj.SRODATA, int64(types.Vaddr+types.Length))
        xdefine("runtime.typelink", obj.SRODATA, int64(typelink.Vaddr))
        xdefine("runtime.etypelink", obj.SRODATA, int64(typelink.Vaddr+typelink.Length))
        xdefine("runtime.itablink", obj.SRODATA, int64(itablink.Vaddr))
 
        xdefine("runtime.eitablink", obj.SRODATA, 0)
        xdefine("runtime.rodata", obj.SRODATA, 0)
        xdefine("runtime.erodata", obj.SRODATA, 0)
+       xdefine("runtime.types", obj.SRODATA, 0)
+       xdefine("runtime.etypes", obj.SRODATA, 0)
        xdefine("runtime.noptrdata", obj.SNOPTRDATA, 0)
        xdefine("runtime.enoptrdata", obj.SNOPTRDATA, 0)
        xdefine("runtime.data", obj.SDATA, 0)
        Addaddr(Ctxt, moduledata, Linklookup(Ctxt, "runtime.end", 0))
        Addaddr(Ctxt, moduledata, Linklookup(Ctxt, "runtime.gcdata", 0))
        Addaddr(Ctxt, moduledata, Linklookup(Ctxt, "runtime.gcbss", 0))
+       Addaddr(Ctxt, moduledata, Linklookup(Ctxt, "runtime.types", 0))
+       Addaddr(Ctxt, moduledata, Linklookup(Ctxt, "runtime.etypes", 0))
        // The typelinks slice
        Addaddr(Ctxt, moduledata, Linklookup(Ctxt, "runtime.typelink", 0))
        adduint(Ctxt, moduledata, uint64(ntypelinks))
 
 
 func TypeLinks() []string {
        var r []string
-       for _, m := range typelinks() {
-               for _, t := range m {
-                       r = append(r, t.string)
+       sections, offset := typelinks()
+       for i, offs := range offset {
+               rodata := sections[i]
+               for _, off := range offs {
+                       r = append(r, rtypeOff(rodata, off).string)
                }
        }
        return r
 
 }
 
 // typelinks is implemented in package runtime.
-// It returns a slice of all the 'typelink' information in the binary,
-// which is to say a slice of known types, sorted by string.
+// It returns a slice of the sections in each module,
+// and a slice of *rtype offsets in each module.
+//
+// The types in each module are sorted by string. That is, the first
+// two linked types of the first module are:
+//
+//     d0 := sections[0]
+//     t1 := (*rtype)(add(d0, offset[0][0]))
+//     t2 := (*rtype)(add(d0, offset[0][1]))
+//
+// and
+//
+//     t1.string < t2.string
+//
 // Note that strings are not unique identifiers for types:
 // there can be more than one with a given string.
 // Only types we might want to look up are included:
 // pointers, channels, maps, slices, and arrays.
-func typelinks() [][]*rtype
+func typelinks() (sections []unsafe.Pointer, offset [][]int32)
+
+func rtypeOff(section unsafe.Pointer, off int32) *rtype {
+       return (*rtype)(add(section, uintptr(off)))
+}
 
 // typesByString returns the subslice of typelinks() whose elements have
 // the given string representation.
 // It may be empty (no known types with that string) or may have
 // multiple elements (multiple types with that string).
 func typesByString(s string) []*rtype {
-       typs := typelinks()
+       sections, offset := typelinks()
        var ret []*rtype
 
-       for _, typ := range typs {
+       for offsI, offs := range offset {
+               section := sections[offsI]
+
                // We are looking for the first index i where the string becomes >= s.
                // This is a copy of sort.Search, with f(h) replaced by (*typ[h].string >= s).
-               i, j := 0, len(typ)
+               i, j := 0, len(offs)
                for i < j {
                        h := i + (j-i)/2 // avoid overflow when computing h
                        // i ≤ h < j
-                       if !(typ[h].string >= s) {
+                       if !(rtypeOff(section, offs[h]).string >= s) {
                                i = h + 1 // preserves f(i-1) == false
                        } else {
                                j = h // preserves f(j) == true
                // Having found the first, linear scan forward to find the last.
                // We could do a second binary search, but the caller is going
                // to do a linear scan anyway.
-               j = i
-               for j < len(typ) && typ[j].string == s {
-                       j++
-               }
-
-               if j > i {
-                       if ret == nil {
-                               ret = typ[i:j:j]
-                       } else {
-                               ret = append(ret, typ[i:j]...)
+               for j := i; j < len(offs); j++ {
+                       typ := rtypeOff(section, offs[j])
+                       if typ.string != s {
+                               break
                        }
+                       ret = append(ret, typ)
                }
        }
        return ret
 
 }
 
 //go:linkname reflect_typelinks reflect.typelinks
-func reflect_typelinks() [][]*_type {
-       ret := [][]*_type{firstmoduledata.typelinks}
+func reflect_typelinks() ([]unsafe.Pointer, [][]int32) {
+       sections := []unsafe.Pointer{unsafe.Pointer(firstmoduledata.types)}
+       ret := [][]int32{firstmoduledata.typelinks}
        for datap := firstmoduledata.next; datap != nil; datap = datap.next {
+               sections = append(sections, unsafe.Pointer(datap.types))
                ret = append(ret, datap.typelinks)
        }
-       return ret
+       return sections, ret
 }
 
        bss, ebss             uintptr
        noptrbss, enoptrbss   uintptr
        end, gcdata, gcbss    uintptr
+       types, etypes         uintptr
 
-       typelinks []*_type
+       typelinks []int32 // offsets from types
        itablinks []*itab
 
        modulename   string