?? RIGHT := 110 ??
MODULE ocm$build_code_sec_directory;
*copyc osd$default_pragmats
?? PUSH (LISTEXT := ON) ??
*copyc oct$code_section_directory
*copyc llt$object_library_header
*copyc llt$module_dictionary
*copyc llt$load_module_header
*copyc llt$object_text_descriptor
*copyc llt$section_address
*copyc llt$section_definition
*copyc llt$segment_definition
*copyc llt$obsolete_segment_definition
?? POP ??

*copyc och$build_code_sec_directory

  PROCEDURE [XDCL] ocp$build_code_sec_directory (p_object_library: ^SEQ ( * );
    VAR code_section_directory: ^oct$code_section_directory;
    VAR module_code_sections: ^oct$module_code_sections);

    VAR
      end_of_section_definitions: boolean,
      first: llt$section_ordinal,
      found: boolean,
      i: llt$module_index,
      j: llt$section_ordinal,
      module_dictionary: ^llt$module_dictionary,
      module_header: ^llt$load_module_header,
      object_library: ^SEQ ( * ),
      object_library_dictionaries: ^llt$object_library_dictionaries,
      object_library_header: ^llt$object_library_header,
      object_text_descriptor: ^llt$object_text_descriptor,
      obsolete_segment_definition: ^llt$obsolete_segment_definition,
      section_definition: ^llt$section_definition,
      segment_definition: ^llt$segment_definition,
      section_definitions: ^llt$object_text_descriptor;

    object_library := p_object_library;

    RESET object_library;
    NEXT object_library_header IN object_library;
    NEXT object_library_dictionaries: [1 .. object_library_header^.number_of_dictionaries] IN object_library;
    found := FALSE;
    i := 1;

    REPEAT
      IF object_library_dictionaries^ [i].kind = llc$module_dictionary THEN
        found := TRUE;
      ELSE
        i := i + 1;
      IFEND;
    UNTIL found OR (i > object_library_header^.number_of_dictionaries);

    IF found THEN
      module_dictionary := #PTR (object_library_dictionaries^ [i].module_dictionary, object_library^);
      ALLOCATE code_section_directory: [1 .. UPPERBOUND (module_dictionary^)];
      ALLOCATE module_code_sections: [1 .. llc$max_section_ordinal];
      j := 1;

      FOR i := 1 TO UPPERBOUND (module_dictionary^) DO
        IF module_dictionary^ [i].kind = llc$load_module THEN
          module_header := #PTR (module_dictionary^ [i].module_header, object_library^);
          IF llc$section_element IN module_header^.interpretive_header.elements_defined THEN
            section_definitions := #PTR (module_header^.interpretive_header.section_definitions,
                  object_library^);
            RESET object_library TO section_definitions;
            first := j;
            end_of_section_definitions := FALSE;

            REPEAT
              NEXT object_text_descriptor IN object_library;
              CASE object_text_descriptor^.kind OF
              = llc$allotted_section_definition =
                NEXT section_definition IN object_library;
                IF section_definition^.kind = llc$code_section THEN
                  module_code_sections^ [j].section_ordinal := section_definition^.section_ordinal;
                  module_code_sections^ [j].start_of_section := #address (#ring (object_library), #segment
                        (object_library), object_text_descriptor^.allotted_section);
                  j := j + 1;
                IFEND;

              = llc$section_definition, llc$unallocated_common_block =
                NEXT section_definition IN object_library;

              = llc$obsolete_allotted_seg_def =
                NEXT obsolete_segment_definition IN object_library;
                IF obsolete_segment_definition^.section_definition.kind = llc$code_section THEN
                  module_code_sections^ [j].section_ordinal := obsolete_segment_definition^.
                        section_definition.section_ordinal;
                  module_code_sections^ [j].start_of_section := #address (#ring (object_library), #segment
                        (object_library), object_text_descriptor^.allotted_segment);
                  j := j + 1;
                IFEND;

              = llc$allotted_segment_definition =
                NEXT segment_definition IN object_library;
                IF segment_definition^.section_definition.kind = llc$code_section THEN
                  module_code_sections^ [j].section_ordinal := segment_definition^.section_definition.
                        section_ordinal;
                  module_code_sections^ [j].start_of_section := #address (#ring (object_library), #segment
                        (object_library), object_text_descriptor^.allotted_segment);
                  j := j + 1;
                IFEND;

              = llc$obsolete_segment_definition =
                NEXT obsolete_segment_definition IN object_library;

              = llc$segment_definition =
                NEXT segment_definition IN object_library;

              ELSE
                end_of_section_definitions := TRUE;
              CASEND;
            UNTIL end_of_section_definitions;
            code_section_directory^ [i].first_entry_number := first;
            code_section_directory^ [i].last_entry_number := j - 1;
          IFEND;
        IFEND;
      FOREND;
    IFEND;
  PROCEND ocp$build_code_sec_directory;
MODEND ocm$build_code_sec_directory;
