@@ -1056,23 +1056,29 @@ static void relaxCGP(Ctx &ctx, const InputSection &sec, size_t i, uint64_t loc,
10561056}
10571057
10581058/* *
1059- * Find all R_RISCV_CHERIOT_COMPARTMENT_LO_I relocations that are CGP-relative
1060- * and rewrite them to be relative to the target of the current relocation.
1061- * These relocations mirror the HI20/LO12 PC-relative relocations and are
1062- * written as pairs where the first has the real relocation target as its
1063- * symbol and the second has the location of the first as its target. This is
1064- * necessary for PC-relative relocations because the final address depends on
1065- * the location of the first instruction. For CHERIoT, both PCC and
1066- * CGP-relative relocations use the same relocation types and we don't know
1067- * whether it is relative to PCC or CGP until we know the target. That would
1068- * be fine, except that relaxation can delete the AUICGP, which means that we
1069- * then can't find the target. We void this by doing a pass to find these
1070- * relocation targets and attaching them to the
1071- * R_RISCV_CHERIOT_COMPARTMENT_LO_I relocations for the cases where the target
1072- * is CGP-relative.
1059+ * Perform a substantial pre-pass of Cheriot relocations ahead of relaxation
1060+ * and relocation. This pre-pass has two goals:
1061+ * - Separate PCC-relative and CGP-relative relocations. This reduces
1062+ * complexity in relaxation and relocation by making the decision once
1063+ * upfront.
1064+ * - Resolve the target address for the second in a pair of relocations. This
1065+ * is required because relaxation may eliminate the first in a pair of
1066+ * relocations, which would leave the second one unable to be relocated.
10731067 *
1074- * Note: If we ever get direct PC[C]-relative loads in RISC-V then other
1075- * relocations will want to reuse this path.
1068+ * The specific post-conditions are:
1069+ * - All PCC-relative CHERIOT_COMPARTMENT_HI relocations are represented as
1070+ * INTERNAL_R_RISCV_CHERIOT_COMPARTMENT_PCCREL_HI.
1071+ * - All CGP-relative CHERIOT_COMPARTMENT_HI relocations are represented as
1072+ * R_RISCV_CHERIOT_COMPARTMENT_HI.
1073+ * - All PCC-relative CHERIOT_COMPARTMENT_LO_I relocations are represented as
1074+ * INTERNAL_R_RISCV_CHERIOT_COMPARTMENT_PCCREL_LO_I.
1075+ * - All CGP-relative CHERIOT_COMPARTMENT_LO_I relocations are represented as
1076+ * R_RISCV_CHERIOT_COMPARTMENT_LO_I.
1077+ * - The targets of all CHERIOT_COMPARTMENT_LO_I relocations are resolved to
1078+ * the target of their paired CHERIOT_COMPARTMENT_HI relocation.
1079+ * - PCC-relative CHERIOT_COMPARTMENT_LO_S relocations do not exist.
1080+ * - All CGP-relative CHERIOT_COMPARTMENT_LO_S relocations are represented as
1081+ * R_RISCV_CHERIOT_COMPARTMENT_LO_S.
10761082 */
10771083static bool rewriteCheriotLowRelocs (Ctx &ctx, InputSection &sec) {
10781084 bool modified = false ;
@@ -1144,6 +1150,8 @@ static bool relax(Ctx &ctx, int pass, InputSection &sec) {
11441150 bool tlsdescRelax = false , toLeShortForm = false ;
11451151
11461152 // On the first pass, do a scan of LO_I CHERIoT relocations
1153+ // FIXME: One the relocation scan loop is under target control, this should be
1154+ // applied outside of relaxation.
11471155 if (pass == 0 )
11481156 changed |= rewriteCheriotLowRelocs (ctx, sec);
11491157
0 commit comments