[4/5] ctf-front-end: Fix representation for multidimensional array type

Message ID 20221117034305.184864-5-guillermo.e.martinez@oracle.com
State New
Headers
Series CTF front-end Bug Fixing and improvement |

Commit Message

Guillermo E. Martinez Nov. 17, 2022, 3:43 a.m. UTC
  To build an IR for multidimensional array the CTF front-end
iterates over the element types recursively, so a definition as
follows:

  char a[2][3][4][5];

Produces: 'char[2][3][4] a[5]' instead of 'char a[2][3][4][5]', it
considers always multidimensional arrays as unidimensional creating
a `array-type-def' node for each dimension:

  <array-type-def dimensions='1' type-id='type-id-1' ... >
    <subrange length='2' type-id='type-id-3' id='type-id-4'/>
  </array-type-def>
  <array-type-def dimensions='1' type-id='type-id-2' ... >
    <subrange length='3' type-id='type-id-3' id='type-id-6'/>
  </array-type-def>
  ...

Instead of:

  <array-type-def dimensions='4' type-id='type-id-1' ... >
     <subrange length='2' type-id='type-id-3' id='type-id-4'/>
     <subrange length='3' type-id='type-id-3' id='type-id-5'/>
     ...
  </array-type-def>

	* src/abg-ctf-reader.cc (+build_array_ctf_range): New definition.
	* tests/data/Makefile.am: Add new testcase.
	* tests/data/test-read-ctf/test-array-mdimension.abi: New testcase.
	* tests/data/test-read-ctf/test-array-mdimension.c: Likewise.
	* tests/data/test-read-ctf/test-array-mdimension.o: Likewise.
	* tests/data/test-read-ctf/test9.o.abi: Adjust.
	* tests/test-read-ctf.cc: Update testsuite.

Signed-off-by: Guillermo E. Martinez <guillermo.e.martinez@oracle.com>
---
 src/abg-ctf-reader.cc                         | 119 ++++++++++++------
 tests/data/Makefile.am                        |   3 +
 .../test-read-ctf/test-array-mdimension.abi   |  16 +++
 .../test-read-ctf/test-array-mdimension.c     |   2 +
 .../test-read-ctf/test-array-mdimension.o     | Bin 0 -> 1360 bytes
 tests/data/test-read-ctf/test9.o.abi          |  36 +++---
 tests/test-read-ctf.cc                        |  27 ++--
 7 files changed, 135 insertions(+), 68 deletions(-)
 create mode 100644 tests/data/test-read-ctf/test-array-mdimension.abi
 create mode 100644 tests/data/test-read-ctf/test-array-mdimension.c
 create mode 100644 tests/data/test-read-ctf/test-array-mdimension.o

index 331bfc70..9f5a7466 100644
  

Comments

Dodji Seketeli Nov. 30, 2022, 9:43 a.m. UTC | #1
Hello Guillermo,

"Guillermo E. Martinez via Libabigail" <libabigail@sourceware.org> a
écrit:

> To build an IR for multidimensional array the CTF front-end
> iterates over the element types recursively, so a definition as
> follows:
>
>   char a[2][3][4][5];
>
> Produces: 'char[2][3][4] a[5]' instead of 'char a[2][3][4][5]', it
> considers always multidimensional arrays as unidimensional creating
> a `array-type-def' node for each dimension:
>
>   <array-type-def dimensions='1' type-id='type-id-1' ... >
>     <subrange length='2' type-id='type-id-3' id='type-id-4'/>
>   </array-type-def>
>   <array-type-def dimensions='1' type-id='type-id-2' ... >
>     <subrange length='3' type-id='type-id-3' id='type-id-6'/>
>   </array-type-def>
>   ...
>
> Instead of:
>
>   <array-type-def dimensions='4' type-id='type-id-1' ... >
>      <subrange length='2' type-id='type-id-3' id='type-id-4'/>
>      <subrange length='3' type-id='type-id-3' id='type-id-5'/>
>      ...
>   </array-type-def>
>
> 	* src/abg-ctf-reader.cc (+build_array_ctf_range): New definition.
> 	* tests/data/Makefile.am: Add new testcase.
> 	* tests/data/test-read-ctf/test-array-mdimension.abi: New testcase.
> 	* tests/data/test-read-ctf/test-array-mdimension.c: Likewise.
> 	* tests/data/test-read-ctf/test-array-mdimension.o: Likewise.
> 	* tests/data/test-read-ctf/test9.o.abi: Adjust.
> 	* tests/test-read-ctf.cc: Update testsuite.

[...]

I believe the patch wasn't applying cleanly because the code in the
master branch changed since your original post. So I had to massage
things a little bit in the hunks touching src/abg-ctf-reader.cc.
Nothing major.

Please find below the resulting patch that I have applied to the master
branch of the repository.

Many thanks!

Cheers,


From e33a74fb8c096bc190704402983acfeca25fc3ee Mon Sep 17 00:00:00 2001
From: "Guillermo E. Martinez" <guillermo.e.martinez@oracle.com>
Date: Wed, 16 Nov 2022 21:43:04 -0600
Subject: [PATCH] ctf-reader: Fix representation of multidimensional arrays

To build an IR for multidimensional array the CTF front-end iterates
over the element types recursively.

So, consider the array definition:

  char a[2][3][4][5];

It's represented as

  'char[2][3][4] a[5]'

instead of:

  'char a[2][3][4][5]'

It always considers multidimensional arrays as unidimensional creating
a `array-type-def' node for each dimension:

  <array-type-def dimensions='1' type-id='type-id-1' ... >
    <subrange length='2' type-id='type-id-3' id='type-id-4'/>
  </array-type-def>
  <array-type-def dimensions='1' type-id='type-id-2' ... >
    <subrange length='3' type-id='type-id-3' id='type-id-6'/>
  </array-type-def>
  ...

Instead of:

  <array-type-def dimensions='4' type-id='type-id-1' ... >
     <subrange length='2' type-id='type-id-3' id='type-id-4'/>
     <subrange length='3' type-id='type-id-3' id='type-id-5'/>
     ...
  </array-type-def>

Fixed thus.

	* src/abg-ctf-reader.cc (+build_array_ctf_range): New definition.
	* tests/data/Makefile.am: Add new testcase.
	* tests/data/test-read-ctf/test-array-mdimension.abi: New testcase.
	* tests/data/test-read-ctf/test-array-mdimension.c: Likewise.
	* tests/data/test-read-ctf/test-array-mdimension.o: Likewise.
	* tests/data/test-read-ctf/test9.o.abi: Adjust.
	* tests/test-read-ctf.cc: Update testsuite.

Signed-off-by: Guillermo E. Martinez <guillermo.e.martinez@oracle.com>
Signed-off-by: Dodji Seketeli <dodji@redhat.com>
---
 src/abg-ctf-reader.cc                         | 101 +++++++++++++-----
 tests/data/Makefile.am                        |   3 +
 .../test-read-ctf/test-array-mdimension.abi   |  16 +++
 .../test-read-ctf/test-array-mdimension.c     |   2 +
 .../test-read-ctf/test-array-mdimension.o     | Bin 0 -> 1360 bytes
 tests/data/test-read-ctf/test9.o.abi          |  36 +++----
 tests/test-read-ctf.cc                        |   9 ++
 7 files changed, 120 insertions(+), 47 deletions(-)
 create mode 100644 tests/data/test-read-ctf/test-array-mdimension.abi
 create mode 100644 tests/data/test-read-ctf/test-array-mdimension.c
 create mode 100644 tests/data/test-read-ctf/test-array-mdimension.o

diff --git a/src/abg-ctf-reader.cc b/src/abg-ctf-reader.cc
index c48e61a4..b4b957d2 100644
--- a/src/abg-ctf-reader.cc
+++ b/src/abg-ctf-reader.cc
@@ -1163,6 +1163,57 @@ process_ctf_union_type(reader *rdr,
   return result;
 }
 
+/// Build and return an array subrange.
+///
+/// @param rdr the read context.
+///
+/// @param ctf_dictionary the CTF dictionary where @ref index
+/// will be found.
+///
+/// @param index the CTF type ID for the array index.
+///
+/// @param nelems the elements number of the array.
+///
+/// @return a shared pointer to subrange built.
+static array_type_def::subrange_sptr
+build_array_ctf_range(reader *rdr, ctf_dict_t *dic,
+                      ctf_id_t index, uint64_t nelems)
+{
+  bool is_infinite = false;
+  corpus_sptr corp = rdr->corpus();
+  translation_unit_sptr tunit = rdr->cur_transl_unit();
+  array_type_def::subrange_sptr subrange;
+  array_type_def::subrange_type::bound_value lower_bound;
+  array_type_def::subrange_type::bound_value upper_bound;
+
+  type_base_sptr index_type = rdr->build_type(dic, index);
+  if (!index_type)
+    return nullptr;
+
+  lower_bound.set_unsigned(0); /* CTF supports C only.  */
+  upper_bound.set_unsigned(nelems > 0 ? nelems - 1 : 0U);
+
+  /* for VLAs number of array elements is 0 */
+  if (upper_bound.get_unsigned_value() == 0)
+    is_infinite = true;
+
+  subrange.reset(new array_type_def::subrange_type(rdr->env(),
+                                                   "",
+                                                   lower_bound,
+                                                   upper_bound,
+                                                   index_type,
+                                                   location(),
+                                                   translation_unit::LANG_C));
+  if (!subrange)
+    return nullptr;
+
+  subrange->is_infinite(is_infinite);
+  add_decl_to_scope(subrange, tunit->get_global_scope());
+  canonicalize(subrange);
+
+  return subrange;
+}
+
 /// Build and return an array type libabigail IR.
 ///
 /// @param rdr the read context.
@@ -1181,7 +1232,6 @@ process_ctf_array_type(reader *rdr,
   translation_unit_sptr tunit = rdr->cur_transl_unit();
   array_type_def_sptr result;
   ctf_arinfo_t ctf_ainfo;
-  bool is_infinite = false;
 
   /* First, get the information about the CTF array.  */
   if (static_cast<ctf_id_t>(ctf_array_info(ctf_dictionary,
@@ -1192,6 +1242,26 @@ process_ctf_array_type(reader *rdr,
   ctf_id_t ctf_element_type = ctf_ainfo.ctr_contents;
   ctf_id_t ctf_index_type = ctf_ainfo.ctr_index;
   uint64_t nelems = ctf_ainfo.ctr_nelems;
+  array_type_def::subrange_sptr subrange;
+  array_type_def::subranges_type subranges;
+
+  int type_array_kind = ctf_type_kind(ctf_dictionary, ctf_element_type);
+  while (type_array_kind == CTF_K_ARRAY)
+    {
+      if (static_cast<ctf_id_t>(ctf_array_info(ctf_dictionary,
+                                               ctf_element_type,
+                                               &ctf_ainfo)) == CTF_ERR)
+        return result;
+
+      subrange = build_array_ctf_range(rdr, ctf_dictionary,
+                                       ctf_ainfo.ctr_index,
+                                       ctf_ainfo.ctr_nelems);
+      subranges.push_back(subrange);
+      ctf_element_type = ctf_ainfo.ctr_contents;
+      type_array_kind = ctf_type_kind(ctf_dictionary, ctf_element_type);
+    }
+
+  std::reverse(subranges.begin(), subranges.end());
 
   /* Make sure the element type is generated.  */
   type_base_sptr element_type = rdr->build_type(ctf_dictionary,
@@ -1210,33 +1280,8 @@ process_ctf_array_type(reader *rdr,
   if (result)
     return result;
 
-  /* The number of elements of the array determines the IR subranges
-     type to build.  */
-  array_type_def::subranges_type subranges;
-  array_type_def::subrange_sptr subrange;
-  array_type_def::subrange_type::bound_value lower_bound;
-  array_type_def::subrange_type::bound_value upper_bound;
-
-  lower_bound.set_unsigned(0); /* CTF supports C only.  */
-  upper_bound.set_unsigned(nelems > 0 ? nelems - 1 : 0U);
-
-  /* for VLAs number of array elements is 0 */
-  if (upper_bound.get_unsigned_value() == 0)
-    is_infinite = true;
-
-  subrange.reset(new array_type_def::subrange_type(rdr->env(),
-                                                   "",
-                                                   lower_bound,
-                                                   upper_bound,
-                                                   index_type,
-                                                   location(),
-                                                   translation_unit::LANG_C));
-  if (!subrange)
-    return result;
-
-  subrange->is_infinite(is_infinite);
-  add_decl_to_scope(subrange, tunit->get_global_scope());
-  canonicalize(subrange);
+  subrange = build_array_ctf_range(rdr, ctf_dictionary,
+                                   ctf_index_type, nelems);
   subranges.push_back(subrange);
 
   /* Finally build the IR for the array type and return it.  */
diff --git a/tests/data/Makefile.am b/tests/data/Makefile.am
index 8d4a2b8f..d04ecf3f 100644
--- a/tests/data/Makefile.am
+++ b/tests/data/Makefile.am
@@ -713,6 +713,9 @@ test-read-ctf/test-bitfield-enum.o	\
 test-read-ctf/test-const-array.abi	\
 test-read-ctf/test-const-array.c	\
 test-read-ctf/test-const-array.o	\
+test-read-ctf/test-array-mdimension.abi	\
+test-read-ctf/test-array-mdimension.c	\
+test-read-ctf/test-array-mdimension.o	\
 \
 test-annotate/test0.abi			\
 test-annotate/test1.abi			\
diff --git a/tests/data/test-read-ctf/test-array-mdimension.abi b/tests/data/test-read-ctf/test-array-mdimension.abi
new file mode 100644
index 00000000..177284d2
--- /dev/null
+++ b/tests/data/test-read-ctf/test-array-mdimension.abi
@@ -0,0 +1,16 @@
+<abi-corpus version='2.1' path='data/test-read-ctf/test-array-mdimension.o'>
+  <elf-variable-symbols>
+    <elf-symbol name='a' size='120' type='object-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
+  </elf-variable-symbols>
+  <abi-instr address-size='64' language='LANG_C'>
+    <type-decl name='char' size-in-bits='8' id='type-id-1'/>
+    <array-type-def dimensions='4' type-id='type-id-1' size-in-bits='960' id='type-id-2'>
+      <subrange length='2' type-id='type-id-3' id='type-id-4'/>
+      <subrange length='3' type-id='type-id-3' id='type-id-5'/>
+      <subrange length='4' type-id='type-id-3' id='type-id-6'/>
+      <subrange length='5' type-id='type-id-3' id='type-id-7'/>
+    </array-type-def>
+    <type-decl name='unsigned long int' size-in-bits='64' id='type-id-3'/>
+    <var-decl name='a' type-id='type-id-2' mangled-name='a' visibility='default' elf-symbol-id='a'/>
+  </abi-instr>
+</abi-corpus>
diff --git a/tests/data/test-read-ctf/test-array-mdimension.c b/tests/data/test-read-ctf/test-array-mdimension.c
new file mode 100644
index 00000000..317fc589
--- /dev/null
+++ b/tests/data/test-read-ctf/test-array-mdimension.c
@@ -0,0 +1,2 @@
+/* gcc -gctf -c test-array-mdimension.c -o test-array-mdimension.o */
+char a[2][3][4][5];
diff --git a/tests/data/test-read-ctf/test-array-mdimension.o b/tests/data/test-read-ctf/test-array-mdimension.o
new file mode 100644
index 0000000000000000000000000000000000000000..5b392d89439a1e39111c350ba2ee8baf7cdb4a14
GIT binary patch
literal 1360
zcmbVM&2G~`5T3Ll{9P!g0vB_l6?pBWT<8H=s-OrVZX7wRoh7mGPqf~u@rhUH8^ELR
z2)qH$051SD_N=`u;lfDcnf>Pbc6Y{3KD~JJa&IyLYZG`5<I17{S9@cgbZG*&;THV-
zJ{>CeM+!_aC%?gd2jdm`27QKp$WA$*H=~IBeZtUYq)jO6?Jfo4(1F9D03E*17)7=Y
zr#ldrB3p-F!v*0KAzMogMP03ei>k?2m0AXQWk5o-u1giAZQ4e)mRX^qMrToxr!vh~
zGB3hPT}DPVriqr)$Sw`FlFKkNANrNlTDD=i%u9uP*OkZs&d<(HgQN4e?~a4{iHJoU
zEaJstK98RTM{2uKn#e{L$Njwnz9$Os&XccBFF22Wz3ZJ(Yu2bfg&)7{+QxgB9KblW
ztu0Vj;f~8o0MWFikts%_`?$85QCkCoW&t8?8W1=KeAFd11fr^q64*xA7@2+K^{ToM
z8(m}DrnRU4cYJ_K>XpBtVV`8k{!IM2J;X9$<3xIX(ucpEzWxz1=)3)wewZ)s6mYcR
z2`2uYR7>cA_S7qSOPC{vu$ah>874k<FiNf7##0A#sh=&u1$S^?BX%9#`F(bZFRb_P
yMYD2^Xug)}-&p^O$v2s2_4I#2Mz}wF!Vh^<`aWFq+d9DFF8vAX-@9VWef@8H9&fb(

literal 0
HcmV?d00001

diff --git a/tests/data/test-read-ctf/test9.o.abi b/tests/data/test-read-ctf/test9.o.abi
index 331bfc70..9f5a7466 100644
--- a/tests/data/test-read-ctf/test9.o.abi
+++ b/tests/data/test-read-ctf/test9.o.abi
@@ -11,47 +11,45 @@
       <subrange length='infinite' type-id='type-id-4' id='type-id-7'/>
     </array-type-def>
     <type-decl name='double' size-in-bits='64' id='type-id-8'/>
-    <array-type-def dimensions='1' type-id='type-id-8' size-in-bits='320' id='type-id-9'>
+    <array-type-def dimensions='2' type-id='type-id-8' size-in-bits='960' id='type-id-9'>
       <subrange length='5' type-id='type-id-4' id='type-id-10'/>
+      <subrange length='3' type-id='type-id-4' id='type-id-11'/>
     </array-type-def>
-    <array-type-def dimensions='1' type-id='type-id-9' size-in-bits='960' id='type-id-11'>
-      <subrange length='3' type-id='type-id-4' id='type-id-12'/>
+    <type-decl name='int' size-in-bits='32' id='type-id-12'/>
+    <array-type-def dimensions='1' type-id='type-id-13' size-in-bits='256' alignment-in-bits='64' id='type-id-14'>
+      <subrange length='4' type-id='type-id-4' id='type-id-15'/>
     </array-type-def>
-    <type-decl name='int' size-in-bits='32' id='type-id-13'/>
-    <array-type-def dimensions='1' type-id='type-id-14' size-in-bits='256' alignment-in-bits='64' id='type-id-15'>
-      <subrange length='4' type-id='type-id-4' id='type-id-16'/>
+    <array-type-def dimensions='1' type-id='type-id-16' size-in-bits='640' alignment-in-bits='64' id='type-id-17'>
+      <subrange length='10' type-id='type-id-4' id='type-id-18'/>
     </array-type-def>
-    <array-type-def dimensions='1' type-id='type-id-17' size-in-bits='640' alignment-in-bits='64' id='type-id-18'>
-      <subrange length='10' type-id='type-id-4' id='type-id-19'/>
-    </array-type-def>
-    <array-type-def dimensions='1' type-id='type-id-13' size-in-bits='160' id='type-id-20'>
+    <array-type-def dimensions='1' type-id='type-id-12' size-in-bits='160' id='type-id-19'>
       <subrange length='5' type-id='type-id-4' id='type-id-10'/>
     </array-type-def>
-    <class-decl name='S' size-in-bits='2304' is-struct='yes' visibility='default' id='type-id-21'>
+    <class-decl name='S' size-in-bits='2304' is-struct='yes' visibility='default' id='type-id-20'>
       <data-member access='public' layout-offset-in-bits='0'>
-        <var-decl name='a' type-id='type-id-20' visibility='default'/>
+        <var-decl name='a' type-id='type-id-19' visibility='default'/>
       </data-member>
       <data-member access='public' layout-offset-in-bits='192'>
         <var-decl name='b' type-id='type-id-3' visibility='default'/>
       </data-member>
       <data-member access='public' layout-offset-in-bits='640'>
-        <var-decl name='c' type-id='type-id-11' visibility='default'/>
+        <var-decl name='c' type-id='type-id-9' visibility='default'/>
       </data-member>
       <data-member access='public' layout-offset-in-bits='1600'>
-        <var-decl name='d' type-id='type-id-18' visibility='default'/>
+        <var-decl name='d' type-id='type-id-17' visibility='default'/>
       </data-member>
       <data-member access='public' layout-offset-in-bits='2240'>
         <var-decl name='e' type-id='type-id-6' visibility='default'/>
       </data-member>
     </class-decl>
     <type-decl name='unsigned long int' size-in-bits='64' id='type-id-4'/>
-    <pointer-type-def type-id='type-id-21' size-in-bits='64' alignment-in-bits='64' id='type-id-22'/>
+    <pointer-type-def type-id='type-id-20' size-in-bits='64' alignment-in-bits='64' id='type-id-21'/>
     <pointer-type-def type-id='type-id-1' size-in-bits='64' alignment-in-bits='64' id='type-id-2'/>
-    <pointer-type-def type-id='type-id-13' size-in-bits='64' alignment-in-bits='64' id='type-id-14'/>
-    <pointer-type-def type-id='type-id-15' size-in-bits='64' alignment-in-bits='64' id='type-id-17'/>
+    <pointer-type-def type-id='type-id-12' size-in-bits='64' alignment-in-bits='64' id='type-id-13'/>
+    <pointer-type-def type-id='type-id-14' size-in-bits='64' alignment-in-bits='64' id='type-id-16'/>
     <function-decl name='foo' visibility='default' binding='global' size-in-bits='64' elf-symbol-id='foo'>
-      <parameter type-id='type-id-22'/>
-      <return type-id='type-id-13'/>
+      <parameter type-id='type-id-21'/>
+      <return type-id='type-id-12'/>
     </function-decl>
   </abi-instr>
 </abi-corpus>
diff --git a/tests/test-read-ctf.cc b/tests/test-read-ctf.cc
index f2d529e2..f3d46118 100644
--- a/tests/test-read-ctf.cc
+++ b/tests/test-read-ctf.cc
@@ -372,6 +372,15 @@ static InOutSpec in_out_specs[] =
     "output/test-read-ctf/test-const-array.abi",
     "--ctf",
   },
+  {
+    "data/test-read-ctf/test-array-mdimension.o",
+    "",
+    "",
+    SEQUENCE_TYPE_ID_STYLE,
+    "data/test-read-ctf/test-array-mdimension.abi",
+    "output/test-read-ctf/test-array-mdimension.abi",
+    "--ctf",
+  },
   // This should be the last entry.
   {NULL, NULL, NULL, SEQUENCE_TYPE_ID_STYLE, NULL, NULL, NULL}
 };
  

Patch

diff --git a/src/abg-ctf-reader.cc b/src/abg-ctf-reader.cc
index 7c267bb1..ba4eed16 100644
--- a/src/abg-ctf-reader.cc
+++ b/src/abg-ctf-reader.cc
@@ -1171,6 +1171,58 @@  process_ctf_union_type(reader *rdr,
   return result;
 }
 
+/// Build and return an array subrange.
+///
+/// @param rdr the read context.
+///
+/// @param ctf_dictionary the CTF dictionary where @ref index
+/// will be found.
+///
+/// @param index the CTF type ID for the array index.
+///
+/// @param nelems the elements number of the array.
+///
+/// @return a shared pointer to subrange built.
+static array_type_def::subrange_sptr
+build_array_ctf_range(reader *rdr, ctf_dict_t *dic,
+                      ctf_id_t index, uint64_t nelems)
+{
+  bool is_infinite = false;
+  corpus_sptr corp = rdr->corpus();
+  translation_unit_sptr tunit = rdr->cur_transl_unit();
+  array_type_def::subrange_sptr subrange;
+  array_type_def::subrange_type::bound_value lower_bound;
+  array_type_def::subrange_type::bound_value upper_bound;
+
+  type_base_sptr index_type = rdr->lookup_type(corp, tunit,
+                                               dic, index);
+  if (!index_type)
+    return nullptr;
+
+  lower_bound.set_unsigned(0); /* CTF supports C only.  */
+  upper_bound.set_unsigned(nelems > 0 ? nelems - 1 : 0U);
+
+  /* for VLAs number of array elements is 0 */
+  if (upper_bound.get_unsigned_value() == 0)
+    is_infinite = true;
+
+  subrange.reset(new array_type_def::subrange_type(rdr->env(),
+                                                   "",
+                                                   lower_bound,
+                                                   upper_bound,
+                                                   index_type,
+                                                   location(),
+                                                   translation_unit::LANG_C));
+  if (!subrange)
+    return nullptr;
+
+  subrange->is_infinite(is_infinite);
+  add_decl_to_scope(subrange, tunit->get_global_scope());
+  canonicalize(subrange);
+
+  return subrange;
+}
+
 /// Build and return an array type libabigail IR.
 ///
 /// @param rdr the read context.
@@ -1189,64 +1241,51 @@  process_ctf_array_type(reader *rdr,
   translation_unit_sptr tunit = rdr->cur_transl_unit();
   array_type_def_sptr result;
   ctf_arinfo_t ctf_ainfo;
-  bool is_infinite = false;
 
   /* First, get the information about the CTF array.  */
   if (static_cast<ctf_id_t>(ctf_array_info(ctf_dictionary,
-					   ctf_type,
-					   &ctf_ainfo)) == CTF_ERR)
+                                           ctf_type,
+                                           &ctf_ainfo)) == CTF_ERR)
     return result;
 
   ctf_id_t ctf_element_type = ctf_ainfo.ctr_contents;
   ctf_id_t ctf_index_type = ctf_ainfo.ctr_index;
   uint64_t nelems = ctf_ainfo.ctr_nelems;
+  array_type_def::subrange_sptr subrange;
+  array_type_def::subranges_type subranges;
+
+  int type_array_kind = ctf_type_kind(ctf_dictionary, ctf_element_type);
+  while (type_array_kind == CTF_K_ARRAY)
+    {
+      if (static_cast<ctf_id_t>(ctf_array_info(ctf_dictionary,
+                                               ctf_element_type,
+                                               &ctf_ainfo)) == CTF_ERR)
+        return result;
+
+      subrange = build_array_ctf_range(rdr, ctf_dictionary,
+                                       ctf_ainfo.ctr_index,
+                                       ctf_ainfo.ctr_nelems);
+      subranges.push_back(subrange);
+      ctf_element_type = ctf_ainfo.ctr_contents;
+      type_array_kind = ctf_type_kind(ctf_dictionary, ctf_element_type);
+    }
+
+  std::reverse(subranges.begin(), subranges.end());
 
   /* Make sure the element type is generated.  */
   type_base_sptr element_type = rdr->lookup_type(corp, tunit,
-						 ctf_dictionary,
-						 ctf_element_type);
+                                                 ctf_dictionary,
+                                                 ctf_element_type);
   if (!element_type)
     return result;
 
-  /* Ditto for the index type.  */
-  type_base_sptr index_type = rdr->lookup_type(corp, tunit,
-					       ctf_dictionary,
-					       ctf_index_type);
-  if (!index_type)
-    return result;
-
   result = dynamic_pointer_cast<array_type_def>(rdr->lookup_type(ctf_dictionary,
-                                                                  ctf_type));
+                                                                 ctf_type));
   if (result)
     return result;
 
-  /* The number of elements of the array determines the IR subranges
-     type to build.  */
-  array_type_def::subranges_type subranges;
-  array_type_def::subrange_sptr subrange;
-  array_type_def::subrange_type::bound_value lower_bound;
-  array_type_def::subrange_type::bound_value upper_bound;
-
-  lower_bound.set_unsigned(0); /* CTF supports C only.  */
-  upper_bound.set_unsigned(nelems > 0 ? nelems - 1 : 0U);
-
-  /* for VLAs number of array elements is 0 */
-  if (upper_bound.get_unsigned_value() == 0)
-    is_infinite = true;
-
-  subrange.reset(new array_type_def::subrange_type(rdr->env(),
-                                                   "",
-                                                   lower_bound,
-                                                   upper_bound,
-                                                   index_type,
-                                                   location(),
-                                                   translation_unit::LANG_C));
-  if (!subrange)
-    return result;
-
-  subrange->is_infinite(is_infinite);
-  add_decl_to_scope(subrange, tunit->get_global_scope());
-  canonicalize(subrange);
+  subrange = build_array_ctf_range(rdr, ctf_dictionary,
+                                   ctf_index_type, nelems);
   subranges.push_back(subrange);
 
   /* Finally build the IR for the array type and return it.  */
diff --git a/tests/data/Makefile.am b/tests/data/Makefile.am
index 8d4a2b8f..d04ecf3f 100644
--- a/tests/data/Makefile.am
+++ b/tests/data/Makefile.am
@@ -713,6 +713,9 @@  test-read-ctf/test-bitfield-enum.o	\
 test-read-ctf/test-const-array.abi	\
 test-read-ctf/test-const-array.c	\
 test-read-ctf/test-const-array.o	\
+test-read-ctf/test-array-mdimension.abi	\
+test-read-ctf/test-array-mdimension.c	\
+test-read-ctf/test-array-mdimension.o	\
 \
 test-annotate/test0.abi			\
 test-annotate/test1.abi			\
diff --git a/tests/data/test-read-ctf/test-array-mdimension.abi b/tests/data/test-read-ctf/test-array-mdimension.abi
new file mode 100644
index 00000000..177284d2
--- /dev/null
+++ b/tests/data/test-read-ctf/test-array-mdimension.abi
@@ -0,0 +1,16 @@ 
+<abi-corpus version='2.1' path='data/test-read-ctf/test-array-mdimension.o'>
+  <elf-variable-symbols>
+    <elf-symbol name='a' size='120' type='object-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
+  </elf-variable-symbols>
+  <abi-instr address-size='64' language='LANG_C'>
+    <type-decl name='char' size-in-bits='8' id='type-id-1'/>
+    <array-type-def dimensions='4' type-id='type-id-1' size-in-bits='960' id='type-id-2'>
+      <subrange length='2' type-id='type-id-3' id='type-id-4'/>
+      <subrange length='3' type-id='type-id-3' id='type-id-5'/>
+      <subrange length='4' type-id='type-id-3' id='type-id-6'/>
+      <subrange length='5' type-id='type-id-3' id='type-id-7'/>
+    </array-type-def>
+    <type-decl name='unsigned long int' size-in-bits='64' id='type-id-3'/>
+    <var-decl name='a' type-id='type-id-2' mangled-name='a' visibility='default' elf-symbol-id='a'/>
+  </abi-instr>
+</abi-corpus>
diff --git a/tests/data/test-read-ctf/test-array-mdimension.c b/tests/data/test-read-ctf/test-array-mdimension.c
new file mode 100644
index 00000000..317fc589
--- /dev/null
+++ b/tests/data/test-read-ctf/test-array-mdimension.c
@@ -0,0 +1,2 @@ 
+/* gcc -gctf -c test-array-mdimension.c -o test-array-mdimension.o */
+char a[2][3][4][5];
diff --git a/tests/data/test-read-ctf/test-array-mdimension.o b/tests/data/test-read-ctf/test-array-mdimension.o
new file mode 100644
index 0000000000000000000000000000000000000000..5b392d89439a1e39111c350ba2ee8baf7cdb4a14
GIT binary patch
literal 1360
zcmbVM&2G~`5T3Ll{9P!g0vB_l6?pBWT<8H=s-OrVZX7wRoh7mGPqf~u@rhUH8^ELR
z2)qH$051SD_N=`u;lfDcnf>Pbc6Y{3KD~JJa&IyLYZG`5<I17{S9@cgbZG*&;THV-
zJ{>CeM+!_aC%?gd2jdm`27QKp$WA$*H=~IBeZtUYq)jO6?Jfo4(1F9D03E*17)7=Y
zr#ldrB3p-F!v*0KAzMogMP03ei>k?2m0AXQWk5o-u1giAZQ4e)mRX^qMrToxr!vh~
zGB3hPT}DPVriqr)$Sw`FlFKkNANrNlTDD=i%u9uP*OkZs&d<(HgQN4e?~a4{iHJoU
zEaJstK98RTM{2uKn#e{L$Njwnz9$Os&XccBFF22Wz3ZJ(Yu2bfg&)7{+QxgB9KblW
ztu0Vj;f~8o0MWFikts%_`?$85QCkCoW&t8?8W1=KeAFd11fr^q64*xA7@2+K^{ToM
z8(m}DrnRU4cYJ_K>XpBtVV`8k{!IM2J;X9$<3xIX(ucpEzWxz1=)3)wewZ)s6mYcR
z2`2uYR7>cA_S7qSOPC{vu$ah>874k<FiNf7##0A#sh=&u1$S^?BX%9#`F(bZFRb_P
yMYD2^Xug)}-&p^O$v2s2_4I#2Mz}wF!Vh^<`aWFq+d9DFF8vAX-@9VWef@8H9&fb(

literal 0
HcmV?d00001

diff --git a/tests/data/test-read-ctf/test9.o.abi b/tests/data/test-read-ctf/test9.o.abi
--- a/tests/data/test-read-ctf/test9.o.abi
+++ b/tests/data/test-read-ctf/test9.o.abi
@@ -11,47 +11,45 @@ 
       <subrange length='infinite' type-id='type-id-4' id='type-id-7'/>
     </array-type-def>
     <type-decl name='double' size-in-bits='64' id='type-id-8'/>
-    <array-type-def dimensions='1' type-id='type-id-8' size-in-bits='320' id='type-id-9'>
+    <array-type-def dimensions='2' type-id='type-id-8' size-in-bits='960' id='type-id-9'>
       <subrange length='5' type-id='type-id-4' id='type-id-10'/>
+      <subrange length='3' type-id='type-id-4' id='type-id-11'/>
     </array-type-def>
-    <array-type-def dimensions='1' type-id='type-id-9' size-in-bits='960' id='type-id-11'>
-      <subrange length='3' type-id='type-id-4' id='type-id-12'/>
+    <type-decl name='int' size-in-bits='32' id='type-id-12'/>
+    <array-type-def dimensions='1' type-id='type-id-13' size-in-bits='256' alignment-in-bits='64' id='type-id-14'>
+      <subrange length='4' type-id='type-id-4' id='type-id-15'/>
     </array-type-def>
-    <type-decl name='int' size-in-bits='32' id='type-id-13'/>
-    <array-type-def dimensions='1' type-id='type-id-14' size-in-bits='256' alignment-in-bits='64' id='type-id-15'>
-      <subrange length='4' type-id='type-id-4' id='type-id-16'/>
+    <array-type-def dimensions='1' type-id='type-id-16' size-in-bits='640' alignment-in-bits='64' id='type-id-17'>
+      <subrange length='10' type-id='type-id-4' id='type-id-18'/>
     </array-type-def>
-    <array-type-def dimensions='1' type-id='type-id-17' size-in-bits='640' alignment-in-bits='64' id='type-id-18'>
-      <subrange length='10' type-id='type-id-4' id='type-id-19'/>
-    </array-type-def>
-    <array-type-def dimensions='1' type-id='type-id-13' size-in-bits='160' id='type-id-20'>
+    <array-type-def dimensions='1' type-id='type-id-12' size-in-bits='160' id='type-id-19'>
       <subrange length='5' type-id='type-id-4' id='type-id-10'/>
     </array-type-def>
-    <class-decl name='S' size-in-bits='2304' is-struct='yes' visibility='default' id='type-id-21'>
+    <class-decl name='S' size-in-bits='2304' is-struct='yes' visibility='default' id='type-id-20'>
       <data-member access='public' layout-offset-in-bits='0'>
-        <var-decl name='a' type-id='type-id-20' visibility='default'/>
+        <var-decl name='a' type-id='type-id-19' visibility='default'/>
       </data-member>
       <data-member access='public' layout-offset-in-bits='192'>
         <var-decl name='b' type-id='type-id-3' visibility='default'/>
       </data-member>
       <data-member access='public' layout-offset-in-bits='640'>
-        <var-decl name='c' type-id='type-id-11' visibility='default'/>
+        <var-decl name='c' type-id='type-id-9' visibility='default'/>
       </data-member>
       <data-member access='public' layout-offset-in-bits='1600'>
-        <var-decl name='d' type-id='type-id-18' visibility='default'/>
+        <var-decl name='d' type-id='type-id-17' visibility='default'/>
       </data-member>
       <data-member access='public' layout-offset-in-bits='2240'>
         <var-decl name='e' type-id='type-id-6' visibility='default'/>
       </data-member>
     </class-decl>
     <type-decl name='unsigned long int' size-in-bits='64' id='type-id-4'/>
-    <pointer-type-def type-id='type-id-21' size-in-bits='64' alignment-in-bits='64' id='type-id-22'/>
+    <pointer-type-def type-id='type-id-20' size-in-bits='64' alignment-in-bits='64' id='type-id-21'/>
     <pointer-type-def type-id='type-id-1' size-in-bits='64' alignment-in-bits='64' id='type-id-2'/>
-    <pointer-type-def type-id='type-id-13' size-in-bits='64' alignment-in-bits='64' id='type-id-14'/>
-    <pointer-type-def type-id='type-id-15' size-in-bits='64' alignment-in-bits='64' id='type-id-17'/>
+    <pointer-type-def type-id='type-id-12' size-in-bits='64' alignment-in-bits='64' id='type-id-13'/>
+    <pointer-type-def type-id='type-id-14' size-in-bits='64' alignment-in-bits='64' id='type-id-16'/>
     <function-decl name='foo' visibility='default' binding='global' size-in-bits='64' elf-symbol-id='foo'>
-      <parameter type-id='type-id-22'/>
-      <return type-id='type-id-13'/>
+      <parameter type-id='type-id-21'/>
+      <return type-id='type-id-12'/>
     </function-decl>
   </abi-instr>
 </abi-corpus>
diff --git a/tests/test-read-ctf.cc b/tests/test-read-ctf.cc
index 61a5682c..a2193303 100644
--- a/tests/test-read-ctf.cc
+++ b/tests/test-read-ctf.cc
@@ -362,15 +362,24 @@  static InOutSpec in_out_specs[] =
     "data/test-read-ctf/test-bitfield-enum.abi",
     "output/test-read-ctf/test-bitfield-enum.abi",
     "--ctf",
-   },
-   {
-     "data/test-read-ctf/test-const-array.o",
-     "",
-     "",
-     SEQUENCE_TYPE_ID_STYLE,
-     "data/test-read-ctf/test-const-array.abi",
-     "output/test-read-ctf/test-const-array.abi",
-     "--ctf",
+  },
+  {
+    "data/test-read-ctf/test-const-array.o",
+    "",
+    "",
+    SEQUENCE_TYPE_ID_STYLE,
+    "data/test-read-ctf/test-const-array.abi",
+    "output/test-read-ctf/test-const-array.abi",
+    "--ctf",
+  },
+  {
+    "data/test-read-ctf/test-array-mdimension.o",
+    "",
+    "",
+    SEQUENCE_TYPE_ID_STYLE,
+    "data/test-read-ctf/test-array-mdimension.abi",
+    "output/test-read-ctf/test-array-mdimension.abi",
+    "--ctf",
   },
   // This should be the last entry.
   {NULL, NULL, NULL, SEQUENCE_TYPE_ID_STYLE, NULL, NULL}